Part Number Hot Search : 
MB95400 R1660CT URAM2H13 TR3D336 R1660CT N4003 K2148 RA152
Product Description
Full Text Search
 

To Download ATTINY20-14 Datasheet File

  If you can't view the Datasheet, Please click here to try to view without PDF Reader .  
 
 


  Datasheet File OCR Text:
  8235f?avr?09/2014 features z high performance, low power 8-bit avr ? microcontroller z advanced risc architecture z 112 powerful instructions ? most single clock cycle execution z 16 x 8 general purpose working registers z fully static operation z up to 12 mips throughput at 12mhz z non-volatile program and data memories z 2k bytes of in-system programmable flash program memory z 128 bytes internal sram z flash write/erase cycles: 10,000 z data retention: 20 years at 85 o c / 100 years at 25 o c z peripheral features z one 8-bit timer/counter with two pwm channels z one 16-bit timer/counter with two pwm channels z 10-bit analog to digital converter z 8 single-ended channels z programmable watchdog timer with separate on-chip oscillator z on-chip analog comparator z master/slave spi serial interface z slave twi serial interface z special microcontroller features z in-system programmable z external and internal interrupt sources z low power idle, adc noise reduction, stand-by and power-down modes z enhanced power-on reset circuit z internal calibrated oscillator z i/o and packages z 14-pin soic/tssop: 12 programmable i/o lines z 12-ball wlcsp: 10 programmable i/o lines z 15-ball ufbga: 12 programmable i/o lines z 20-pad vqfn: 12 programmable i/o lines z operating voltage: z 1.8 ? 5.5v z programming voltage: z 5v z speed grade z 0 ? 4mhz @ 1.8 ? 5.5v z 0 ? 8mhz @ 2.7 ? 5.5v z 0 ? 12mhz @ 4.5 ? 5.5v z industrial temperature range z low power consumption z active mode: z 200 a at 1mhz and 1.8v z idle mode: z 25 a at 1mhz and 1.8v z power-down mode: z < 0.1 a at 1.8v attiny20 8-bit avr microcontroller with 2k bytes in-system programmable flash datasheet
2 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 1. pin configurations 1.1 soic & tssop figure 1-1. soic/tssop 1.2 vqfn figure 1-2. vqfn 1 2 3 4 5 6 7 14 13 12 11 10 9 8 vcc (pcint8/tpiclk/t0/clki) pb0 (pcint9/tpidata/mosi/sda/oc1a) pb1 (pcint11/reset) pb3 (pcint10/int0/miso/oc1b/oc0a/ckout) pb2 (pcint7/scl/sck/t1/icp1/oc0b/adc7) pa7 (pcint6/ss/adc6) pa6 gnd pa0 (adc0/pcint0) pa1 (adc1/ain0/pcint1) pa2 (adc2/ain1/pcint2) pa3 (adc3/pcint3) pa4 (adc4/pcint4) pa5 (adc5/pcint5) 1 2 3 4 5 15 14 13 12 11 20 19 18 17 16 6 7 8 9 10 note bottom pad should be soldered to ground. dnc: do not connect dnc dnc gnd vcc dnc pa7 (adc7/oc0b/icp1/t1/scl/sck/pcint7) pb2 (ckout/oc0a/oc1b/miso/int0/pcint10) pb3 (reset/pcint11) pb1 (oc1a/sda/mosi/tpidata/pcint9) pb0 (clki/t0/tpiclk/pcint8) dnc dnc dnc pa5 (adc5/pcint5) pa6 (adc6/pcint6/ss) (pcint4/adc4) pa4 (pcint3/adc3) pa3 (pcint2/ain1/adc2) pa2 (pcint1/ain0/adc1) pa1 (pcint0/adc0) pa0
3 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 1.3 ufbga figure 1-3. ufbga table 1-1. ufbga pin configuration 1.4 wafer level chip scale package figure 1-4. wlcsp table 1-2. wlcsp ball configuration 1 2 3 4 a pa5 pa6 pb2 b pa4 pa7 pb1 pb3 c pa3 pa2 pa1 pb0 d pa0 gnd gnd vcc a b c d 1234 a b c d 4321 top view bottom view 1 2 3 4 5 6 a pa4 pa1 pa2 b pa6 gnd vdd c pa5 pa7 pb1 d pb2 pb3 pb0 a b c d 1234 a b c d 654321 top view bottom view
4 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 1.5 pin description 1.5.1 vcc supply voltage. 1.5.2 gnd ground. 1.5.3 reset reset input. a low level on this pin for longer than the minimum pulse length will generate a reset, even if the clock is not running and provided the reset pin has not been disabled. the minimum pulse length is given in table 20-4 on page 170 . shorter pulses are not guaranteed to generate a reset. the reset pin can also be used as a (weak) i/o pin. 1.5.4 port a (pa7:pa0) port a is a 8-bit bi-directional i/o port with internal pull-up resistors (selected for each bit). the port a output buffers ha ve symmetrical drive characteristics with both high sink and source capability. as inputs, port a pins that are externally pulled low will source current if the pull-up resistors are activated. the port a pins are tri-stated when a reset condition becomes active, even if the clock is not running. port a has alternate functions as analog inputs for the adc, analog comparator and pin change interrupt as described in ?alternate port functions? on page 47 . 1.5.5 port b (pb3:pb0) port b is a 4-bit bi-directional i/o port with internal pull-up resistors (selected for each bit). the port b output buffers ha ve symmetrical drive characteristics with both high sink and source capability except pb3 which has the reset capability. to use pin pb3 as an i/o pin, instead of reset pin, program (?0?) rstdisbl fuse. as inputs, port b pins that are externally pulled low will source current if the pull-up resistors are activated. the port b pins are tri-stated when a reset condition becomes active, even if the clock is not running. the port also serves the functions of various special features of the attiny20, as listed on page 37 .
5 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 2. overview attiny20 is a low-power cmos 8-bit microcontroller based on the compact avr enhanced risc architecture. by executing powerful instructions in a single clock cycle, the attiny20 achieves throughputs approaching 1 mips per mhz allowing the system designer to optimize power consumption versus processing speed. figure 2-1. block diagram stack pointer sram program counter programming logic isp interface internal oscillator watchdog timer reset flag register mcu status register timer/ counter0 calibrated oscillator timing and control interrupt unit analog comparator adc general purpose registers x y z alu status register program flash instruction register instruction decoder control lines v cc reset data register port a direction reg. port a drivers port a gnd pa[7:0] 8-bit data bus timer/ counter1 twi spi data register port b direction reg. port b drivers port b pb[3:0]
6 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 the avr core combines a rich instruction set with 16 general purpose working registers and system registers. all registers are directly connected to the arithmetic logic unit (alu), allowing two independent registers to be accessed in one single instruction executed in one clock cycle. the resulting architecture is compact and code efficient while achieving throughputs up to ten times faster than conventional cisc microcontrollers. attiny20 provides the following features: z 2k bytes of in-system programmable flash z 128 bytes of sram z twelve general purpose i/o lines z 16 general purpose working registers z an 8-bit timer/counter with two pwm channels z a 16-bit timer/counter with two pwm channels z internal and external interrupts z an eight-channel, 10-bit adc z a programmable watchdog timer with internal oscillator z a slave two-wire interface z a master/slave serial peripheral interface z an internal calibrated oscillator z four software selectable power saving modes the device includes the following modes for saving power: z idle mode: stops the cpu while allowing the timer/counter, adc, analog comparator, spi, twi, and interrupt system to continue functioning z adc noise reduction mode: minimizes switching noise during adc conversions by stopping the cpu and all i/o modules except the adc z power-down mode: registers keep their contents and all chip functions are disabled until the next interrupt or hardware reset z standby mode: the oscillator is running while the rest of the device is sleeping, allowing very fast start-up combined with low power consumption. the device is manufactured using atmel?s high density non-volatile memory technology. the on-chip, in-system programmable flash allows program memory to be re-programmed in-system by a conventional, non-volatile memory programmer. the attiny20 avr is supported by a suite of program and system development tools, including macro assemblers and evaluation kits.
7 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 3. general information 3.1 resources a comprehensive set of drivers, application notes, data sheets and descriptions on development tools are available for download at http://www.atmel.com/avr. 3.2 code examples this documentation contains simple code examples that briefly show how to use various parts of the device. these code examples assume that the part specific header file is included before compilation. be aware that not all c compiler vendors include bit definitions in the header files and interrupt handling in c is compiler dependent. please confirm with the c compiler documentation for more details. 3.3 capacitive touch sensing atmel qtouch library provides a simple to use solution for touch sensitive interfaces on atmel avr microcontrollers. the qtouch library includes support for qtouch ? and qmatrix ? acquisition methods. touch sensing is easily added to any application by linking the qtouch library and using the application programming interface (api) of the library to define the touch channels and sensors. the application then calls the api to retrieve channel information and determine the state of the touch sensor. the qtouch library is free and can be downloaded from the atmel website. for more information and details of implementation, refer to the qtouch library user guide ? also available from the atmel website. 3.4 data retention reliability qualification results show that the projected data retention failure rate is much less than 1 ppm over 20 years at 85c or 100 years at 25c. 3.5 disclaimer typical values contained in this datasheet are based on simulations and characterization of other avr microcontrollers manufactured on the same process technology.
8 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 4. cpu core this section discusses the avr core architecture in general. the main function of the cpu core is to ensure correct program execution. the cpu must therefore be able to access memories, perform calculations, control peripherals, and handle interrupts. 4.1 architectural overview figure 4-1. block diagram of the avr architecture in order to maximize performance and parallelism, the avr uses a harvard architecture ? with separate memories and buses for program and data. instructions in the program memory are executed with a single level pipelining. while one instruction is being executed, the next instruction is pre-fetched from the program memory. this concept enables instructions to be executed in every clock cycle. the program memory is in-system reprogrammable flash memory. the fast-access register file contains 16 x 8-bit general purpose working registers with a single clock cycle access time. this allows single-cycle arithmetic logic unit (alu) operation. in a typical alu operation, two operands are output from the register file, the operation is executed, and the result is stored back in the register file ? in one clock cycle. flash program memory instruction register instruction decoder program counter control lines 16 x 8 general purpose registrers alu status and control i/o lines data bus 8-bit data sram direct addressing indirect addressing interrupt unit watchdog timer analog comparator timer/counter 0 adc twi slave spi timer/counter 1
9 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 six of the 16 registers can be used as three 16-bit indirect address register pointers for data space addressing ? enabling efficient address calculations. these added function registers are the 16-bit x-, y-, and z-register, described later in this section. the alu supports arithmetic and logic operations between registers or between a constant and a register. single register operations can also be executed in the alu. after an arithmetic operation, the status register is updated to reflect information about the result of the operation. program flow is provided by conditional and unconditional jump and call instructions, capable of directly addressing the whole address space. most avr instructions have a single 16-bit word format but 32-bit wide instructions also exist. the actual instruction set varies, as some devices only implement a part of the instruction set. during interrupts and subroutine calls, the return address program counter (pc) is stored on the stack. the stack is effectively allocated in the general data sram, and consequently the stack size is only limited by the sram size and the usage of the sram. all user programs must initialize the sp in the reset routine (before subroutines or interrupts are executed). the stack pointer (sp) is read/write accessible in the i/o space. the data sram can easily be accessed through the four different addressing modes supported in the avr architecture. the memory spaces in the avr architecture are all linear and regular memory maps. a flexible interrupt module has its control registers in the i/o space with an additional global interrupt enable bit in the status register. all interrupts have a separate interrupt vector in the interrupt vector table. the interrupts have priority in accordance with their interrupt vector position. the lower the interrupt vector address, the higher the priority. the i/o memory space contains 64 addresses for cpu peripheral functions as control registers, spi, and other i/o functions. the i/o memory can be accessed as the data space locations, 0x0000 - 0x003f. 4.2 alu ? arithmetic logic unit the high-performance avr alu operates in direct connection with all the 16 general purpose working registers. within a single clock cycle, arithmetic operations between general purpose registers or between a register and an immediate are executed. the alu operations are divided into three main categories ? arithmetic, logical, and bit-functions. some implementations of the architecture also provide a powerful multiplier supporting both signed/unsigned multiplication and fractional format. see document ?avr instruction set? and section ?instruction set summary? on page 205 for a detailed description. 4.3 status register the status register contains information about the result of the most recently executed arithmetic instruction. this information can be used for altering program flow in order to perform conditional operations. note that the status register is updated after all alu operations, as specified in document ?avr instruction set? and section ?instruction set summary? on page 205 . this will in many cases remove the need for using the dedicated compare instructions, resulting in faster and more compact code. the status register is not automatically stored when entering an interrupt routine and restored when returning from an interrupt. this must be handled by software. 4.4 general purpose register file the register file is optimized for the avr enhanced risc instruction set. in order to achieve the required performance and flexibility, the following input/output schemes are supported by the register file: z one 8-bit output operand and one 8-bit result input z two 8-bit output operands and one 8-bit result input z one 16-bit output operand and one 16-bit result input figure 4-2 below shows the structure of the 16 general purpose working registers in the cpu.
10 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 4-2. avr cpu general purpose working registers note: a typical implementation of the avr register file includes 32 general purpose registers but attiny20 implements only 16 registers. for reasons of compatibility the registers are numbered r16:r31 and not r0:r15. most of the instructions operating on the register file have direct access to all registers, and most of them are single cycle instructions. 4.4.1 the x-register, y-register, and z-register registers r26:r31 have some added functions to their general purpose usage. these registers are 16-bit address pointers for indirect addressing of the data space. the three indirect address registers x, y, and z are defined as described in figure 4-3 . figure 4-3. the x-, y-, and z-registers in different addressing modes these address registers function as automatic increment and automatic decrement (see document ?avr instruction set? and section ?instruction set summary? on page 205 for details). 4.5 stack pointer the stack is mainly used for storing temporary data, local variables and return addresses after interrupts and subroutine calls. the stack pointer registers (sph and spl) always point to the top of the stack. note that the stack grows from higher memory locations to lower memory locations. this means that the push instructions decreases and the pop instruction increases the stack pointer value. 70 r16 r16 r17 r17 general r18 r18 purpose ? ... working r26 r26 x-register low byte registers r27 r27 x-register high byte r28 r28 y-register low byte r29 r29 y-register high byte r30 r30 z-register low byte r31 r31 z-register high byte 15 xh xl 0 x-register 707 0 r27 r26 15 yh yl 0 y-register 707 0 r29 r28 15 zh zl 0 z-register 707 0 r31 r30
11 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 the stack pointer points to the area of data memory where subroutine and interrupt stacks are located. this stack space must be defined by the program before any subroutine calls are executed or interrupts are enabled. the pointer is decremented by one when data is put on the stack with the push instruction, and incremented by one when data is fetched with the pop instruction. it is decremented by two when the return address is put on the stack by a subroutine call or a jump to an interrupt service routine, and incremented by two when data is fetched by a return from subroutine (the ret instruction) or a return from interrupt service routine (the reti instruction). the avr stack pointer is typically implemented as two 8-bit registers in the i/o register file. the width of the stack pointer and the number of bits implemented is device dependent. in some avr devices all data memory can be addressed using spl, only. in this case, the sph register is not implemented. the stack pointer must be set to point above the i/o register areas, the minimum value being the lowest address of sram. see figure 5-1 on page 15 . 4.6 instruction execution timing this section describes the general access timing concepts for instruction execution. the avr cpu is driven by the cpu clock clk cpu , directly generated from the selected clock source for the chip. no internal clock division is used. figure 4-4. the parallel instruction fetches and instruction executions figure 4-4 shows the parallel instruction fetches and instruction executions enabled by the harvard architecture and the fast access register file concept. this is the basic pipelining concept to obtain up to 1 mips per mhz with the corresponding unique results for functions per cost, functions per clocks, and functions per power-unit. figure 4-5 shows the internal timing concept for the register file. in a single clock cycle an alu operation using two register operands is executed, and the result is stored back to the destination register. figure 4-5. single cycle alu operation clk 1st instruction fetch 1st instruction execute 2nd instruction fetch 2nd instruction execute 3rd instruction fetch 3rd instruction execute 4th instruction fetch t1 t2 t3 t4 cpu total execution time register operands fetch alu operation execute result write back t1 t2 t3 t4 clk cpu
12 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 4.7 reset and interrupt handling the avr provides several different interrupt sources. these interrupts and the separate reset vector each have a separate program vector in the program memory space. all interrupts are assigned individual enable bits which must be written logic one together with the global interrupt enable bit in the status register in order to enable the interrupt. the lowest addresses in the program memory space are by default defined as the reset and interrupt vectors. the complete list of vectors is shown in ?interrupts? on page 36 . the list also determines the priority levels of the different interrupts. the lower the address the higher is the priority level. reset has the highest priority, and next is int0 ? the external interrupt request 0. when an interrupt occurs, the global interrupt enable i-bit is cleared and all interrupts are disabled. the user software can write logic one to the i-bit to enable nested interrupts. all enabled interrupts can then interrupt the current interrupt routine. the i-bit is automatically set when a return from interrupt instruction ? reti ? is executed. there are basically two types of interrupts. the first type is triggered by an event that sets the interrupt flag. for these interrupts, the program counter is vectored to the actual interrupt vector in order to execute the interrupt handling routine, and hardware clears the corresponding interrupt flag. interrupt flags can also be cleared by writing a logic one to the flag bit position(s) to be cleared. if an interrupt condition occurs while the corresponding interrupt enable bit is cleared, the interrupt flag will be set and remembered until the interrupt is enabled, or the flag is cleared by software. similarly, if one or more interrupt conditions occur while the global interrupt enable bit is cleared, the corresponding interrupt flag(s) will be set and remembered until the global interrupt enable bit is set, and will then be executed by order of priority. the second type of interrupts will trigger as long as the interrupt condition is present. these interrupts do not necessarily have interrupt flags. if the interrupt condition disappears before the interrupt is enabled, the interrupt will not be triggered. when the avr exits from an interrupt, it will always return to the main program and execute one more instruction before any pending interrupt is served. note that the status register is not automatically stored when entering an interrupt routine, nor restored when returning from an interrupt routine. this must be handled by software. when using the cli instruction to disable interrupts, the interrupts will be immediately disabled. no interrupt will be executed after the cli instruction, even if it occurs simultaneously with the cli instruction. when using the sei instruction to enable interrupts, the instruction following sei will be executed before any pending interrupts, as shown in the following example. note: see ?code examples? on page 7 . 4.7.1 interrupt response time the interrupt execution response for all the enabled avr interrupts is four clock cycles minimum. after four clock cycles the program vector address for the actual interrupt handling routine is executed. during this four clock cycle period, the program counter is pushed onto the stack. the vector is normally a jump to the interrupt routine, and this jump takes three clock cycles. if an interrupt occurs during execution of a multi-cycle instruction, this instruction is completed before the interrupt is served. if an interrupt occurs when the mcu is in sleep mode, the interrupt execution response time is increased by four clock cycles. this increase comes in addition to the start-up time from the selected sleep mode. a return from an interrupt handling routine takes four clock cycles. during these four clock cycles, the program counter (two bytes) is popped back from the stack, the stack pointer is incremented by two, and the i-bit in sreg is set. assembly code example sei ; set global interrupt enable sleep ; enter sleep, waiting for interrupt ; note: will enter sleep before any pending interrupt(s)
13 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 4.8 register description 4.8.1 ccp ? configuration change protection register z bits 7:0 ? ccp[7:0]: configuration change protection in order to change the contents of a protected i/o register the ccp register must first be written with the correct signature. after ccp is written the protected i/o registers may be written to during the next four cpu instruction cycles. all interrupts are ignored during these cycles. after these cycles interrupts are automatically handled again by the cpu, and any pending interrupts will be executed according to their priority. when the protected i/o register signature is written, ccp0 will read as one as long as the protected feature is enabled, while ccp[7:1] will always read as zero. table 4-1 shows the signatures that are recognized. table 4-1. signatures recognized by the configuration change protection register notes: 1. only wde and wdp[3:0] bits are protected in wdtcsr. 2. only bods bit is protected in mcucr. 4.8.2 sph and spl ? stack pointer registers z bits 7:0 ? sp[7:0]: stack pointer the stack pointer register points to the top of the stack, which is implemented growing from higher memory locations to lower memory locations. hence, a stack push command decreases the stack pointer. the stack space in the data sram must be defined by the program before any subroutine calls are executed or interrupts are enabled. in attiny20, the sph register has not been implemented. bit 76543210 0x3c ccp[7:0] ccp read/write wwwwwwwr/w initial value 00000000 signature group description 0xd8 ioreg: clkmsr, clkpsr, wdtcsr (1) , mcucr (2) protected i/o register initial value 00000000 read/write r r rrrrrr bit 15 14 13 12 11 10 9 8 0x3e ???????? sph 0x3d sp7 sp6 sp5 sp4 sp3 sp2 sp1 sp0 spl bit 76543210 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value ramend ramend ramend ramend ramend ramend ramend ramend
14 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 4.8.3 sreg ? status register z bit 7 ? i: global interrupt enable the global interrupt enable bit must be set for the interrupts to be enabled. the individual interrupt enable control is then performed in separate control registers. if the global interrupt enable register is cleared, none of the interrupts are enabled independent of the individual interrupt enable settings. the i-bit is cleared by hardware after an interrupt has occurred, and is set by the reti instruction to enable subsequent interrupts. the i-bit can also be set and cleared by the application with the sei and cli instructions, as described in the document ?avr instruction set? and ?instruction set summary? on page 205 . z bit 6 ? t: bit copy storage the bit copy instructions bld (bit load) and bst (bit store) use the t-bit as source or destination for the operated bit. a bit from a register in the register file can be copied into t by the bst instruction, and a bit in t can be copied into a bit in a register in the register file by the bld instruction. z bit 5 ? h: half carry flag the half carry flag h indicates a half carry in some arithmetic operations. half carry is useful in bcd arithmetic. see document ?avr instruction set? and section ?instruction set summary? on page 205 for detailed information. z bit 4 ? s: sign bit, s = n v the s-bit is always an exclusive or between the negative flag n and the two?s complement overflow flag v. see document ?avr instruction set? and section ?instruction set summary? on page 205 for detailed information. z bit 3 ? v: two?s complement overflow flag the two?s complement overflow flag v supports two?s complement arithmetic. see document ?avr instruction set? and section ?instruction set summary? on page 205 for detailed information. z bit 2 ? n: negative flag the negative flag n indicates a negative result in an arithmetic or logic operation. see document ?avr instruction set? and section ?instruction set summary? on page 205 for detailed information. z bit 1 ? z: zero flag the zero flag z indicates a zero result in an arithmetic or logic operation. see document ?avr instruction set? and section ?instruction set summary? on page 205 for detailed information. z bit 0 ? c: carry flag the carry flag c indicates a carry in an arithmetic or logic operation. see document ?avr instruction set? and section ?instruction set summary? on page 205 for detailed information. bit 76543210 0x3f i t h s v n z c sreg read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 00000000
15 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 5. memories this section describes the different memories in the attiny20. the device has two main memory areas, the program memory space and the data memory space. 5.1 in-system re-programmable flash program memory the attiny20 contains 2k byte on-chip, in-system reprogrammable flash memory for program storage. since all avr instructions are 16 or 32 bits wide, the flash is organized as 1024 x 16. the flash memory has an endurance of at least 10,000 write/erase cycles. the attiny20 program counter (pc) is 10 bits wide, thus capable of addressing the 1024 program memory locations, starting at 0x000. ?memory programming? on page 159 contains a detailed description on flash data serial downloading. constant tables can be allocated within the entire address space of program memory. since program memory can not be accessed directly, it has been mapped to the data memory. the mapped program memory begins at byte address 0x4000 in data memory (see figure 5-1 on page 15 ). although programs are executed starting from address 0x000 in program memory it must be addressed starting from 0x4000 when accessed via the data memory. internal write operations to flash program memory have been disabled and program memory therefore appears to firmware as read-only. flash memory can still be written to externally but internal write operations to the program memory area will not be successful. timing diagrams of instruction fetch and execution are presented in ?instruction execution timing? on page 11 . 5.2 data memory data memory locations include the i/o memory, the internal sram memory, the non-volatile memory lock bits, and the flash memory. see figure 5-1 for an illustration on how the attiny20 memory space is organized. figure 5-1. data memory map (byte addressing) 0x0000 ... 0x003f 0x0040 ... 0x00bf 0x00c0 ... 0x3eff 0x3f00 ... 0x3f01 0x3f02 ... 0x3f3f 0x3f40 ... 0x3f41 0x3f42 ... 0x3f7f 0x3f80 ... 0x3f81 0x3f82 ... 0x3fbf 0x3fc0 ... 0x3fc3 0x3fc4 ... 0x3fff 0x4000 ... 0x47ff 0x4800 ... 0xffff i/o space sram data memory (reserved) nvm lock bits (reserved) configuration bits (reserved) calibration bits (reserved) device id bits (reserved) flash program memory (reserved)
16 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 the first 64 locations are reserved for i/o memory, while the following 128 data memory locations (from 0x0040 to 0x00bf) address the internal data sram. the non-volatile memory lock bits and all the flash memory sections are mapped to the data memory space. these locations appear as read-only for device firmware. the four different addressing modes for data memory are direct, indirect, indirect with pre-decrement, and indirect with post-increment. in the register file, registers r26 to r31 function as pointer registers for indirect addressing. the in and out instructions can access all 64 locations of i/o memory. direct addressing using the lds and sts instructions reaches the 128 locations between 0x0040 and 0x00bf. the indirect addressing reaches the entire data memory space. when using indirect addressing modes with automatic pre-decrement and post-increment, the address registers x, y, and z are decremented or incremented. 5.2.1 data memory access times this section describes the general access timing concepts for internal memory access. the internal data sram access is performed in two clk cpu cycles as described in figure 5-2 . figure 5-2. on-chip data sram access cycles 5.3 i/o memory the i/o space definition of the attiny20 is shown in ?register summary? on page 203 . all attiny20 i/os and peripherals are placed in the i/o space. all i/o locations may be accessed using the ld and st instructions, enabling data transfer between the 16 general purpose working registers and the i/o space. i/o registers within the address range 0x00 - 0x1f are directly bit-accessible using the sbi and cbi instructions. in these registers, the value of single bits can be checked by using the sbis and sbic instructions. see document ?avr instruction set? and section ?instruction set summary? on page 205 for more details. when using the i/o specific commands in and out, the i/o addresses 0x00 - 0x3f must be used. for compatibility with future devices, reserved bits should be written to zero if accessed. reserved i/o memory addresses should never be written. some of the status flags are cleared by writing a logical one to them. note that cbi and sbi instructions will only operate on the specified bit, and can therefore be used on registers containing such status flags. the cbi and sbi instructions work on registers in the address range 0x00 to 0x1f, only. the i/o and peripherals control registers are explained in later sections. clk wr rd data data address address valid t1 t2 t3 compute address read write cpu memory access instruction next instruction
17 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 6. clock system figure 6-1 presents the principal clock systems and their distribution in attiny20. all of the clocks need not be active at a given time. in order to reduce power consumption, the clocks to modules not being used can be halted by using different sleep modes and power reduction register bits, as described in ?power management and sleep modes? on page 23 . the clock systems is detailed below. figure 6-1. clock distribution 6.1 clock subsystems the clock subsystems are detailed in the sections below. 6.1.1 cpu clock ? clk cpu the cpu clock is routed to parts of the system concerned with operation of the avr core. examples of such modules are the general purpose register file, the system registers and the sram data memory. halting the cpu clock inhibits the core from performing general operations and calculations. 6.1.2 i/o clock ? clk i/o the i/o clock is used by the majority of the i/o modules, like timer/counter. the i/o clock is also used by the external interrupt module, but note that some external interrupts are detected by asynchronous logic, allowing such interrupts to be detected even if the i/o clock is halted. 6.1.3 nvm clock - clk nvm the nvm clock controls operation of the non-volatile memory controller. the nvm clock is usually active simultaneously with the cpu clock. clock control unit general i/o modules analog-to-digital converter cpu core watchdog timer reset logic clock prescaler ram clock switch nvm calibrated oscillator clk adc source clock clk i/o clk cpu clk nvm watchdog clock watchdog oscillator external clock
18 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 6.1.4 adc clock ? clk adc the adc is provided with a dedicated clock domain. this allows halting the cpu and i/o clocks in order to reduce noise generated by digital circuitry. this gives more accurate adc conversion results. 6.2 clock sources all synchronous clock signals are derived from the main clock. the device has three alternative sources for the main clock, as follows: z calibrated internal 8 mhz oscillator (see page 18 ) z external clock (see page 18 ) z internal 128 khz oscillator (see page 18 ) see table 6-3 on page 21 on how to select and change the active clock source. 6.2.1 calibrated internal 8 mhz oscillator the calibrated internal oscillator provides an approximately 8 mhz clock signal. though voltage and temperature dependent, this clock can be very accurately calibrated by the user. see table 20-2 on page 169 , and ?internal oscillator speed? on page 200 for more details. this clock may be selected as the main clock by setting the clock main select bits clkms[1:0] in clkmsr to 0b00. once enabled, the oscillator will operate with no external components. during reset, hardware loads the calibration byte into the osccal register and thereby automatically calibrates the oscillator. the accuracy of this calibration is shown as factory calibration in table 20-2 on page 169 . when this oscillator is used as the main clock, the watchdog oscillator will still be used for the watchdog timer and reset time-out. for more information on the pre-programmed calibration value, see section ?calibration section? on page 162 . 6.2.2 external clock to use the device with an external clock source, clki should be driven as shown in figure 6-2 . the external clock is selected as the main clock by setting clkms[1:0] bits in clkmsr to 0b10. figure 6-2. external clock drive configuration when applying an external clock, it is required to avoid sudden changes in the applied clock frequency to ensure stable operation of the mcu. a variation in frequency of more than 2% from one clock cycle to the next can lead to unpredictable behavior. it is required to ensure that the mcu is kept in reset during such changes in the clock frequency. 6.2.3 internal 128 khz oscillator the internal 128 khz oscillator is a low power oscillator providing a clock of 128 khz. the frequency depends on supply voltage, temperature and batch variations. this clock may be select as the main clock by setting the clkms[1:0] bits in clkmsr to 0b01. external clock signal clki gnd
19 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 6.2.4 switching clock source the main clock source can be switched at run-time using the ?clkmsr ? clock main settings register? on page 20 . when switching between any clock sources, the clock system ensures that no glitch occurs in the main clock. 6.2.5 default clock source the calibrated internal 8 mhz oscillator is always selected as main clock when the device is powered up or has been reset. the synchronous system clock is the main clock divided by 8, controlled by the system clock prescaler. the clock prescaler select bits can be written later to change the system clock frequency. see ?system clock prescaler?. 6.3 system clock prescaler the system clock is derived from the main clock via the system clock prescaler. the system clock can be divided by setting the ?clkpsr ? clock prescale register? on page 21 . the system clock prescaler can be used to decrease power consumption at times when requirements for processing power is low or to bring the system clock within limits of maximum frequency. the prescaler can be used with all main clock source options, and it will affect the clock frequency of the cpu and all synchronous peripherals. the system clock prescaler can be used to implement run-time changes of the internal clock frequency while still ensuring stable operation. 6.3.1 switching prescaler setting when switching between prescaler settings, the system clock prescaler ensures that no glitch occurs in the system clock and that no intermediate frequency is higher than neither the clock frequency corresponding the previous setting, nor the clock frequency corresponding to the new setting. the ripple counter that implements the prescaler runs at the frequency of the main clock, which may be faster than the cpu's clock frequency. hence, it is not possible to determine the state of the prescaler - even if it were readable, and the exact time it takes to switch from one clock division to another cannot be exactly predicted. from the time the clkps values are written, it takes between t1 + t2 and t1 + 2*t2 before the new clock frequency is active. in this interval, two active clock edges are produced. here, t1 is the previous clock period, and t2 is the period corresponding to the new prescaler setting. 6.4 starting 6.4.1 starting from reset the internal reset is immediately asserted when a reset source goes active. the internal reset is kept asserted until the reset source is released and the start-up sequence is completed. the start-up sequence includes three steps, as follows. 1. the first step after the reset source has been released consists of the device counting the reset start-up time. the purpose of this reset start-up time is to ensure that supply voltage has reached sufficient levels. the reset start-up time is counted using the internal 128 khz oscillator. see table 6-1 for details of reset start-up time. note that the actual supply voltage is not monitored by the start-up logic. the device will count until the reset start- up time has elapsed even if the device has reached sufficient supply voltage levels earlier. 2. the second step is to count the oscillator start-up time, which ensures that the calibrated internal oscillator has reached a stable state before it is used by the other parts of the system. the calibrated internal oscillator needs to oscillate for a minimum number of cycles before it can be considered stable. see table 6-1 for details of the oscil- lator start-up time. 3. the last step before releasing the internal reset is to load the calibration and the configuration values from the non-volatile memory to configure the device properly. the configuration time is listed in table 6-1 .
20 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 table 6-1. start-up times when using the internal calibrated oscillator notes: 1. after powering up the device or after a reset the system clock is automatically set to calibrated internal 8 mhz oscillator, divided by 8 2. when the brown-out detection is enabled, the reset start-up time is 128 ms after powering up the device. 6.4.2 starting from power-down mode when waking up from power-down sleep mode, the supply voltage is assumed to be at a sufficient level and only the oscillator start-up time is counted to ensure the stable operation of the oscillator. the oscillator start-up time is counted on the selected main clock, and the start-up time depends on the clock selected. see table 6-2 for details. table 6-2. start-up time from power-down sleep mode notes: 1. the start-up time is measured in main clock oscillator cycles. 2. when using software bod disable, the wake-up time from sleep mode will be approximately 60 s. 6.4.3 starting from idle / adc noise reduction / standby mode when waking up from idle, adc noise reduction or standby mode, the oscillator is already running and no oscillator start-up time is introduced. 6.5 register description 6.5.1 clkmsr ? clock main settings register z bits 7:2 ? res: reserved bits these bits are reserved and will always read as zero. z bits 1:0 ? clkms[1:0]: clock main select bits these bits select the main clock source of the system. the bits can be written at run-time to switch the source of the main clock. the clock system ensures glitch free switching of the main clock source. the main clock alternatives are shown in table 6-3 . reset oscillator configuration total start-up time 64 ms 6 cycles 21 cycles 64 ms + 6 oscillator cycles + 21 system clock cycles (1)(2) oscillator start-up time total start-up time 6 cycles 6 oscillator cycles (1)(2) bit 765432 1 0 0x37 ? ? ? ? ? ? clkms1 clkms0 clkmsr read/write r r r r r r r/w r/w initial value 0 0 0 0 0 0 0 0
21 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 table 6-3. selection of main clock to avoid unintentional switching of main clock source, a protected change sequence must be followed to change the clkms bits, as follows: 1. write the signature for change enable of protected i/o register to register ccp 2. within four instruction cycles, write the clkms bits with the desired value 6.5.2 clkpsr ? clock prescale register z bits 7:4 ? res: reserved bits these bits are reserved and will always read as zero. z bits 3:0 ? clkps[3:0]: clock prescaler select bits 3 - 0 these bits define the division factor between the selected clock source and the internal system clock. these bits can be written at run-time to vary the clock frequency and suit the application requirements. as the prescaler divides the master clock input to the mcu, the speed of all synchronous peripherals is reduced accordingly. the division factors are given in table 6-4 . table 6-4. clock prescaler select clkm1 clkm0 main clock source 0 0 calibrated internal 8 mhz oscillator 0 1 internal 128 khz oscillator (wdt oscillator) 1 0 external clock 1 1 reserved bit 76543210 0x36 ? ? ? ? clkps3 clkps2 clkps1 clkps0 clkpsr read/write rrrrr/wr/wr/wr/w initial value 0 0 0 0 0011 clkps3 clkps2 clkps1 clkps0 clock division factor 0 0 0 0 1 0 0 0 1 2 0 0 1 0 4 0 0 1 1 8 (default) 0 1 0 0 16 0 1 0 1 32 0 1 1 0 64 0 1 1 1 128 1 0 0 0 256 1 0 0 1 reserved
22 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 to avoid unintentional changes of clock frequency, a protected change sequence must be followed to change the clkps bits: 1. write the signature for change enable of protected i/o register to register ccp 2. within four instruction cycles, write the desired value to clkps bits at start-up, the clkps bits will be reset to 0b0011 to select the clock division factor of 8. the application software must ensure that a sufficient division factor is chosen if the selected clock source has a higher frequency than the maximum frequency of the device at the present operating conditions. 6.5.3 osccal ? oscillator calibration register . z bits 7:0 ? cal[7:0]: oscillator calibration value the oscillator calibration register is used to trim the calibrated internal oscillator and remove process variations from the oscillator frequency. a pre-programmed calibration value is automatically written to this register during chip reset, giving the factory calibrated frequency as specified in table 20-2, ?calibration accuracy of internal rc oscillator,? on page 169 . the application software can write this register to change the oscillator frequency. the oscillator can be calibrated to frequencies as specified in table 20-2, ?calibration accuracy of internal rc oscillator,? on page 169 . calibration outside the range given is not guaranteed. the cal[7:0] bits are used to tune the frequency of the oscillator. a setting of 0x00 gives the lowest frequency, and a setting of 0xff gives the highest frequency. 1 0 1 0 reserved 1 0 1 1 reserved 1 1 0 0 reserved 1 1 0 1 reserved 1 1 1 0 reserved 1 1 1 1 reserved clkps3 clkps2 clkps1 clkps0 clock division factor bit 76543210 0x39 cal7 cal6 cal5 cal4 cal3 cal2 cal1 cal0 osccal read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
23 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 7. power management and sleep modes the high performance and industry leading code efficiency makes the avr microcontrollers an ideal choice for low power applications. in addition, sleep modes enable the application to shut down unused modules in the mcu, thereby saving power. the avr provides various sleep modes allowing the user to tailor the power consumption to the application?s requirements. 7.1 sleep modes figure 6-1 on page 17 presents the different clock systems and their distribution in attiny20. the figure is helpful in selecting an appropriate sleep mode. table 7-1 shows the different sleep modes and their wake up sources. table 7-1. active clock domains and wake-up sources in different sleep modes notes: 1. for int0, only level interrupt. 2. only twi address match interrupt. to enter any of the four sleep modes, the se bits in mcucr must be written to logic one and a sleep instruction must be executed. the sm[2:0] bits in the mcucr register select which sleep mode (idle, adc noise reduction, standby or power-down) will be activated by the sleep instruction. see table 7-2 for a summary. if an enabled interrupt occurs while the mcu is in a sleep mode, the mcu wakes up. the mcu is then halted for four cycles in addition to the start-up time, executes the interrupt routine, and resumes execution from the instruction following sleep. the contents of the register file and sram are unaltered when the device wakes up from sleep. if a reset occurs during sleep mode, the mcu wakes up and executes from the reset vector. note that if a level triggered interrupt is used for wake-up the changed level must be held for some time to wake up the mcu (and for the mcu to enter the interrupt service routine). see ?external interrupts? on page 37 for details. 7.1.1 idle mode when bits sm[2:0] are written to 000, the sleep instruction makes the mcu enter idle mode, stopping the cpu but allowing the analog comparator, adc, timer/counters, watchdog, twi, spi and the interrupt system to continue operating. this sleep mode basically halts clk cpu and clk nvm , while allowing the other clocks to run. idle mode enables the mcu to wake up from external triggered interrupts as well as internal ones like the timer overflow. if wake-up from the analog comparator interrupt is not required, the analog comparator can be powered down by setting the acd bit in ?acsra ? analog comparator control and status register? on page 106 . this will reduce power consumption in idle mode. if the adc is enabled, a conversion starts automatically when this mode is entered. sleep mode active clock domains oscillators wake-up sources clk cpu clk nvm clk io clk adc main clock source enabled int0 and pin change watchdog interrupt twi slave adc other i/o idle x x x x x x x x adc noise reduction x x x (1) x x (2) x standby x x (1) x x (2) power-down x (1) x x (2)
24 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 7.1.2 adc noise reduction mode when bits sm[2:0] are written to 001, the sleep instruction makes the mcu enter adc noise reduction mode, stopping the cpu but allowing the adc, the external interrupts, twi and the watchdog to continue operating (if enabled). this sleep mode halts clk i/o , clk cpu , and clk nvm , while allowing the other clocks to run. this mode improves the noise environment for the adc, enabling higher resolution measurements. if the adc is enabled, a conversion starts automatically when this mode is entered. 7.1.3 power-down mode when bits sm[2:0] are written to 010, the sleep instruction makes the mcu enter power-down mode. in this mode, the oscillator is stopped, while the external interrupts, twi and the watchdog continue operating (if enabled). only a watchdog reset, an external level interrupt on int0, a pin change interrupt, or a twi slave interrupt can wake up the mcu. this sleep mode halts all generated clocks, allowing operation of asynchronous modules only. 7.1.4 standby mode when bits sm[2:0] are written to 100, the sleep instruction makes the mcu enter standby mode. this mode is identical to power-down with the exception that the oscillator is kept running. this reduces wake-up time, because the oscillator is already running and doesn't need to be started up. 7.2 software bod disable when the brown-out detector (bod) is enabled by bodlevel fuses (see table 19-5 on page 161 ), the bod is actively monitoring the supply voltage during a sleep period. in some devices it is possible to save power by disabling the bod by software in power-down and stand-by sleep modes. the sleep mode power consumption will then be at the same level as when bod is globally disabled by fuses. if bod is disabled by software, the bod function is turned off immediately after entering the sleep mode. upon wake-up from sleep, bod is automatically enabled again. this ensures safe operation in case the v cc level has dropped during the sleep period. when the bod has been disabled, the wake-up time from sleep mode will be approximately 60 s to ensure that the bod is working correctly before the mcu continues executing code. bod disable is controlled by the bods (bod sleep) bit of mcu control register, see ?mcucr ? mcu control register? on page 26 . writing this bit to one turns off bod in power-down and stand-by, while writing a zero keeps the bod active. the default setting is zero, i.e. bod active. writing to the bods bit is controlled by a timed sequence, see ?mcucr ? mcu control register? on page 26 . 7.3 power reduction register the power reduction register (prr), see ?prr ? power reduction register? on page 27 , provides a method to reduce power consumption by stopping the clock to individual peripherals. when the clock for a peripheral is stopped then: z the current state of the peripheral is frozen. z the associated registers can not be read or written. z resources used by the peripheral will remain occupied. the peripheral should in most cases be disabled before stopping the clock. clearing the prr bit wakes up the peripheral and puts it in the same state as before shutdown. peripheral shutdown can be used in idle mode and active mode to significantly reduce the overall power consumption. see ?supply current of i/o modules? on page 174 for examples. in all other sleep modes, the clock is already stopped.
25 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 7.4 minimizing power consumption there are several issues to consider when trying to minimize the power consumption in an avr core controlled system. in general, sleep modes should be used as much as possible, and the sleep mode should be selected so that as few as possible of the device?s functions are operating. all functions not needed should be disabled. in particular, the following modules may need special consideration when trying to achieve the lowest possible power consumption. 7.4.1 analog comparator when entering idle mode, the analog comparator should be disabled if not used. in the power-down mode, the analog comparator is automatically disabled. see ?analog comparator? on page 105 for further details. 7.4.2 analog to digital converter if enabled, the adc will be enabled in all sleep modes. to save power, the adc should be disabled before entering any sleep mode. when the adc is turned off and on again, the next conversion will be an extended conversion. see ?analog to digital converter? on page 109 for details on adc operation. 7.4.3 watchdog timer if the watchdog timer is not needed in the application, this module should be turned off. if the watchdog timer is enabled, it will be enabled in all sleep modes, and hence, always consume power. in the deeper sleep modes, this will contribute significantly to the total current consumption. refer to ?watchdog timer? on page 31 for details on how to configure the watchdog timer. 7.4.4 brown-out detector if the brown-out detector is not needed in the application, this module should be turned off. if the brown-out detector is enabled by the bodlevel fuses, it will be enabled in all sleep modes, and hence, always consume power. in the deeper sleep modes, this will contribute significantly to the total current consumption. see ?brown-out detection? on page 30 and ?software bod disable? on page 24 for details on how to configure the brown-out detector. 7.4.5 port pins when entering a sleep mode, all port pins should be configured to use minimum power. the most important thing is then to ensure that no pins drive resistive loads. in sleep modes where the i/o clock (clk i/o ) is stopped, the input buffers of the device will be disabled. this ensures that no power is consumed by the input logic when not needed. in some cases, the input logic is needed for detecting wake-up conditions, and it will then be enabled. refer to the section ?digital input enable and sleep modes? on page 46 for details on which pins are enabled. if the input buffer is enabled and the input signal is left floating or has an analog signal level close to v cc /2, the input buffer will use excessive power. for analog input pins, the digital input buffer should be disabled at all times. an analog signal level close to v cc /2 on an input pin can cause significant current even in active mode. digital input buffers can be disabled by writing to the digital input disable register (didr0). refer to ?didr0 ? digital input disable register 0? on page 108 for details.
26 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 7.5 register description 7.5.1 mcucr ? mcu control register the mcu control register contains bits for controlling external interrupt sensing and power management. z bit 5 ? res: reserved bit this bit is reserved and will always read as zero. z bit 4 ? bods: bod sleep in order to disable bod during sleep (see table 7-1 on page 23 ) the bods bit must be written to logic one. this is controlled by a protected change sequence, as follows: 1. write the signature for change enable of protected i/o registers to register ccp. 2. within four instruction cycles write the bods bit. a sleep instruction must be executed while bods is active in order to turn off the bod for the actual sleep mode. the bods bit is automatically cleared when the device wakes up. alternatively the bods bit can be cleared by writing logic zero to it. this does not require protected sequence. z bits 3:1 ? sm[2:0]: sleep mode select bits 2 - 0 these bits select between available sleep modes, as shown in table 7-2 . table 7-2. sleep mode select z bit 0 ? se: sleep enable the se bit must be written to logic one to make the mcu enter the sleep mode when the sleep instruction is executed. to avoid the mcu entering the sleep mode unless it is the programmer?s purpose, it is recommended to write the sleep enable (se) bit to one just before the execution of the sleep instruction and to clear it immediately after waking up. bit 76543210 0x3a isc01 isc00 ? bods sm2 sm1 sm0 se mcucr read/write r/w r/w r r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 sm2 sm1 sm0 sleep mode 0 0 0 idle 0 0 1 adc noise reduction 0 1 0 power-down 0 1 1 reserved 1 0 0 standby 1 0 1 reserved 1 1 0 reserved 1 1 1 reserved
27 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 7.5.2 prr ? power reduction register z bits 7:5 ? res: reserved bits these bits are reserved and will always read as zero. z bit 4 ? prtwi: power reduction two-wire interface writing a logic one to this bit shuts down the two-wire interface module. z bit 3 ? prspi: power reduction serial peripheral interface writing a logic one to this bit shuts down the serial peripheral interface module. z bit 2 ? prtim1: power reduction timer/counter1 writing a logic one to this bit shuts down the timer/counter1 module. when the timer/counter1 is enabled, operation will continue like before the shutdown. z bit 1 ? prtim0: power reduction timer/counter0 writing a logic one to this bit shuts down the timer/counter0 module. when the timer/counter0 is enabled, operation will continue like before the shutdown. z bit 0 ? pradc: power reduction adc writing a logic one to this bit shuts down the adc. the adc must be disabled before shut down. the analog comparator cannot use the adc input mux when the adc is shut down. bit 7 6 5 4 3 2 1 0 0x35 ? ? ? prtwi prspi prtim1 prtim0 pradc prr read/write r r r r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
28 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 8. system control and reset 8.1 resetting the avr during reset, all i/o registers are set to their initial values, and the program starts execution from the reset vector. the instruction placed at the reset vector must be a rjmp ? relative jump ? instruction to the reset handling routine. if the program never enables an interrupt source, the interrupt vectors are not used, and regular program code can be placed at these locations. the circuit diagram in figure 8-1 shows the reset logic. electrical parameters of the reset circuitry are defined in section ?system and reset characteristics? on page 170 . figure 8-1. reset logic the i/o ports of the avr are immediately reset to their initial state when a reset source goes active. this does not require any clock source to be running. after all reset sources have gone inactive, a delay counter is invoked, stretching the internal reset. this allows the power to reach a stable level before normal operation starts. the start up sequence is described in ?starting from reset? on page 19 . 8.2 reset sources the attiny20 has four sources of reset: z power-on reset. the mcu is reset when the supply voltage is below the power-on reset threshold (v pot ) z external reset. the mcu is reset when a low level is present on the reset pin for longer than the minimum pulse length z watchdog reset. the mcu is reset when the watchdog timer period expires and the watchdog is enabled z brown out reset. the mcu is reset when the brown-out detector is enabled and supply voltage is below the brown-out threshold (v bot ) 8.2.1 power-on reset a power-on reset (por) pulse is generated by an on-chip detection circuit. the detection level is defined in section ?system and reset characteristics? on page 170 . the por is activated whenever v cc is below the detection level. the por circuit can be used to trigger the start-up reset, as well as to detect a failure in supply voltage. a power-on reset (por) circuit ensures that the device is reset from power-on. reaching the power-on reset threshold voltage invokes the delay counter, which determines how long the device is kept in reset after v cc rise. the reset signal is activated again, without any delay, when v cc decreases below the detection level. data b u s reset flag register r e s e t f l a g r e g i s t e r (rstflr) ( r s t f l r ) power-on p o w e r - o n reset circuit r e s e t c i r c u i t pull-up p u l l - u p resistor r e s i s t o r bodlevel2...0 b o d l e v e l 2 . . . 0 v cc c c spike s p i k e filter f i l t e r reset r e s e t external e x t e r n a l reset circuit r e s e t c i r c u i t brown out b r o w n o u t reset circuit r e s e t c i r c u i t rstdisbl r s t d i s b l watchdog w a t c h d o g timer t i m e r delay d e l a y counters c o u n t e r s s r q watchdog w a t c h d o g oscillator o s c i l l a t o r clock c l o c k generator g e n e r a t o r borf porf extrf wdrf internal i n t e r n a l reset r e s e t ck c k timeout t i m e o u t counter re s et c o u n t e r r e s e t
29 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 8-2. mcu start-up, reset tied to v cc figure 8-3. mcu start-up, reset extended externally 8.2.2 external reset an external reset is generated by a low level on the reset pin if enabled. reset pulses longer than the minimum pulse width (see section ?system and reset characteristics? on page 170 ) will generate a reset, even if the clock is not running. shorter pulses are not guaranteed to generate a reset. when the applied signal reaches the reset threshold voltage ? v rst ? on its positive edge, the delay counter starts the mcu after the time-out period ? t tout ? has expired. external reset is ignored during power-on start-up count. after power-on reset the internal reset is extended only if reset pin is low when the initial power-on delay count is complete. see figure 8-2 and figure 8-3 . v t ime-out reset reset tout internal t v pot v rst cc v t ime-out tout tout internal cc t v pot v rst > t reset reset
30 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 8-4. external reset during operation 8.2.3 watchdog reset when the watchdog times out, it will generate a short reset pulse. on the falling edge of this pulse, the delay timer starts counting the time-out period t tout . see page 30 for details on operation of the watchdog timer and table 20-4 on page 170 for details on reset time-out. figure 8-5. watchdog reset during operation 8.2.4 brown-out detection attiny20 has an on-chip brown-out detection (bod) circuit for monitoring the v cc level during operation by comparing it to a fixed trigger level. the trigger level for the bod can be selected by the bodlevel fuses. the trigger level has a hysteresis to ensure spike free brown-out detection. the hysteresis on the detection level should be interpreted as v bot+ = v bot + v hyst /2 and v bot- = v bot - v hyst /2. when the bod is enabled, and v cc decreases to a value below the trigger level (v bot- in figure 8-6 on page 31 ), the brown-out reset is immediately activated. when v cc increases above the trigger level (v bot+ in figure 8-6 ), the delay counter starts the mcu after the time-out period t tout has expired. cc ck cc
31 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 the bod circuit will only detect a drop in v cc if the voltage stays below the trigger level for longer than t bod given in ?system and reset characteristics? on page 170 . figure 8-6. brown-out reset during operation 8.3 internal voltage reference attiny20 features an internal bandgap reference. this reference is used for brown-out detection, and it can be used as an input to the analog comparator or the adc. the bandgap voltage varies with supply voltage and temperature. 8.3.1 voltage reference enable signals and start-up time the voltage reference has a start-up time that may influence the way it should be used. the start-up time is given in ?system and reset characteristics? on page 170 . to save power, the reference is not always turned on. the reference is on during the following situations: 1. when the bod is enabled (by programming the bodlevel[2:0] fuse). 2. when the internal reference is connected to the analog comparator (by setting the acbg bit in acsr). 3. when the adc is enabled. thus, when the bod is not enabled, after setting the acbg bit or enabling the adc, the user must always allow the reference to start up before the output from the analog comparator or adc is used. to reduce power consumption in power-down mode, the user can avoid the three conditions above to ensure that the reference is turned off before entering power-down mode. 8.4 watchdog timer the watchdog timer is clocked from an on-chip oscillator, which runs at 128 khz. see figure 8-7 on page 32 . by controlling the watchdog timer prescaler, the watchdog reset interval can be adjusted as shown in table 8-2 on page 34 . the wdr ? watchdog reset ? instruction resets the watchdog timer. the watchdog timer is also reset when it is disabled and when a device reset occurs. ten different clock cycle periods can be selected to determine the reset period. if the reset period expires without another watchdog reset, the attiny20 resets and executes from the reset vector. for timing details on the watchdog reset, refer to table 8-3 on page 34 . v cc reset time-out internal reset v bot- v bot+ t tout
32 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 8-7. watchdog timer the watchdog timer can also be configured to generate an interrupt instead of a reset. this can be very helpful when using the watchdog to wake-up from power-down. to prevent unintentional disabling of the watchdog or unintentional change of time-out period, two different safety levels are selected by the fuse wdton as shown in table 8-1 on page 32 . see ?procedure for changing the watchdog timer configuration? on page 32 for details. table 8-1. wdt configuration as a function of the fuse settings of wdton 8.4.1 procedure for changing the watchdog timer configuration the sequence for changing configuration differs between the two safety levels, as follows: 8.4.1.1 safety level 1 in this mode, the watchdog timer is initially disabled, but can be enabled by writing the wde bit to one without any restriction. a special sequence is needed when disabling an enabled watchdog timer. to disable an enabled watchdog timer, the following procedure must be followed: 1. write the signature for change enable of protected i/o registers to register ccp 2. within four instruction cycles, in the same operation, write wde and wdp bits 8.4.1.2 safety level 2 in this mode, the watchdog timer is always enabled, and the wde bit will always read as one. a protected change is needed when changing the watchdog time-out period. to change the watchdog time-out, the following procedure must be followed: 1. write the signature for change enable of protected i/o registers to register ccp 2. within four instruction cycles, write the wdp bit. the value written to wde is irrelevant wdton safety level initial state how to disable how to change time-out unprogrammed 1 disabled protected change sequence no limitations programmed 2 enabled always enabled protected change sequence osc/2k osc/4k osc/8k osc/16k osc/32k osc/64k osc/128k osc/256k osc/512k osc/1024k mcu reset watchdog prescaler 128 khz oscillator watchdog reset wdp0 wdp1 wdp2 wdp3 wde mux
33 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 8.4.2 code examples the following code example shows how to turn off the wdt. the example assumes that interrupts are controlled (for example by disabling interrupts globally) so that no interrupts will occur during execution of these functions. note: see ?code examples? on page 7 . 8.5 register description 8.5.1 wdtcsr ? watchdog timer control and status register z bit 7 ? wdif: watchdog timer interrupt flag this bit is set when a time-out occurs in the watchdog timer and the watchdog timer is configured for interrupt. wdif is cleared by hardware when executing the corresponding interrupt handling vector. alternatively, wdif is cleared by writing a logic one to the flag. when the wdie is set, the watchdog time-out interrupt is requested. z bit 6 ? wdie: watchdog timer interrupt enable when this bit is written to one, the watchdog interrupt request is enabled. if wde is cleared in combination with this setting, the watchdog timer is in interrupt mode, and the corresponding interrupt is requested if time-out in the watchdog timer occurs. if wde is set, the watchdog timer is in interrupt and system reset mode. the first time-out in the watchdog timer will set wdif. executing the corresponding interrupt vector will clear wdie and wdif automatically by hardware (the watchdog goes to system reset mode). this is useful for keeping the watchdog timer security while using the interrupt. to stay in interrupt and system reset mode, wdie must be set after each interrupt. this should however not be done within the interrupt service routine itself, as this might compromise the safety-function of the watchdog system reset mode. if the interrupt is not executed before the next time-out, a system reset will be applied. assembly code example wdt_off: wdr ; clear wdrf in rstflr in r16, rstflr andi r16, ~(1< 34 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 table 8-2. watchdog timer configuration note: 1. wdton configuration bit set to ?0? means programmed and ?1? means unprogrammed. z bit 4 ? res: reserved bit this bit is reserved and will always read as zero. z bit 3 ? wde: watchdog system reset enable wde is overridden by wdrf in rstflr. this means that wde is always set when wdrf is set. to clear wde, wdrf must be cleared first. this feature ensures multiple resets during conditions causing failure, and a safe start-up after the failure. z bits 5, 2:0 ? wdp[3:0]: watchdog timer prescaler 3 - 0 the wdp[3:0] bits determine the watchdog timer prescaling when the watchdog timer is running. the different prescaling values and their corresponding time-out periods are shown in table 8-3 on page 34 . table 8-3. watchdog timer prescale select wdton (1) wde wdie mode action on time-out 1 0 0 stopped none 1 0 1 interrupt interrupt 1 1 0 system reset reset 1 1 1 interrupt and system reset interrupt, then go to system reset mode 0 x x system reset reset wdp3 wdp2 wdp1 wdp0 number of wdt oscillator cycles typical time-out at v cc = 5.0v 0 0 0 0 2k (2048) cycles 16 ms 0 0 0 1 4k (4096) cycles 32 ms 0 0 1 0 8k (8192) cycles 64 ms 0 0 1 1 16k (16384) cycles 0.125 s 0 1 0 0 32k (32768) cycles 0.25 s 0 1 0 1 64k (65536) cycles 0.5 s 0 1 1 0 128k (131072) cycles 1.0 s 0 1 1 1 256k (262144) cycles 2.0 s 1 0 0 0 512k (524288) cycles 4.0 s 1 0 0 1 1024k (1048576) cycles 8.0 s
35 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 8.5.2 rstflr ? reset flag register the reset flag register provides information on which reset source caused an mcu reset. z bits 7:4 ? res: reserved bits these bits are reserved and will always read as zero. z bit 3 ? wdrf: watchdog reset flag this bit is set if a watchdog reset occurs. the bit is reset by a power-on reset, or by writing a logic zero to the flag. z bit 2 ? borf: brown-out reset flag this bit is set if a brown-out reset occurs. the bit is reset by a power-on reset, or by writing a logic zero to the flag. z bit 1 ? extrf: external reset flag this bit is set if an external reset occurs. the bit is reset by a power-on reset, or by writing a logic zero to the flag. z bit 0 ? porf: power-on reset flag this bit is set if a power-on reset occurs. the bit is reset only by writing a logic zero to the flag. to make use of the reset flags to identify a reset condition, the user should read and then reset the rstflr as early as possible in the program. if the register is cleared before another reset occurs, the source of the reset can be found by examining the reset flags. 1 0 1 0 reserved 1 0 1 1 1 1 0 0 1 1 0 1 1 1 1 0 1 1 1 1 wdp3 wdp2 wdp1 wdp0 number of wdt oscillator cycles typical time-out at v cc = 5.0v bit 76543210 0x3b ???? wdrf borf extrf porf rstflr read/write r r r r r/w r/w r/w r/w initial value 0000xxxx
36 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 9. interrupts this section describes the specifics of the interrupt handling as performed in attiny20. for a general explanation of the avr interrupt handling, see ?reset and interrupt handling? on page 12 . 9.1 interrupt vectors the interrupt vectors of attiny20 are described in table 9-1 below. table 9-1. reset and interrupt vectors in case the program never enables an interrupt source, the interrupt vectors will not be used and, consequently, regular program code can be placed at these locations. vector no. program address label interrupt source 1 0x0000 reset external pin, power-on reset, brown-out reset, watchdog reset 2 0x0001 int0 external interrupt request 0 3 0x0002 pcint0 pin change interrupt request 0 4 0x0003 pcint1 pin change interrupt request 1 5 0x0004 wdt watchdog time-out 6 0x0005 tim1_capt timer/counter1 input capture 7 0x0006 tim1_compa timer/counter1 compare match a 8 0x0007 tim1_compb timer/counter1 compare match b 9 0x0008 tim1_ovf timer/counter1 overflow 10 0x0009 tim0_compa timer/counter0 compare match a 11 0x000a tim0_compb timer/counter0 compare match b 12 0x000b tim0_ovf timer/counter0 overflow 13 0x000c ana_comp analog comparator 14 0x000d adc adc conversion complete 15 0x000e twi_slave two-wire interface 16 0x000f spi serial peripheral interface 17 0x0010 qtrip (1) 1. the touch sensing interrupt source is related to the qtouch library support. touch sensing
37 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 a typical and general setup for interrupt vector addresses in attiny20 is shown in the program example below. note: see ?code examples? on page 7 . 9.2 external interrupts external interrupts are triggered by the int0 pin or any of the pcint[11:0] pins. observe that, if enabled, the interrupts will trigger even if the int0 or pcint[11:0] pins are configured as outputs. this feature provides a way of generating a software interrupt. pin change 0 interrupts pci0 will trigger if any enabled pcint[7:0] pin toggles. pin change 1 interrupts pci1 will trigger if any enabled pcint[11:8] pin toggles. the pcmsk0 and pcmsk1 registers control which pins contribute to the pin change interrupts. pin change interrupts on pcint[11:0] are detected asynchronously, which means that these interrupts can be used for waking the part also from sleep modes other than idle mode. the int0 interrupt can be triggered by a falling or rising edge or a low level. this is set up as shown in ?mcucr ? mcu control register? on page 38 . when the int0 interrupt is enabled and configured as level triggered, the interrupt will trigger as long as the pin is held low. note that recognition of falling or rising edge interrupts on int0 requires the presence of an i/o clock, as described in ?clock system? on page 17 . 9.2.1 low level interrupt a low level interrupt on int0 is detected asynchronously. this means that the interrupt source can be used for waking the part also from sleep modes other than idle (the i/o clock is halted in all sleep modes except idle). note that if a level triggered interrupt is used for wake-up from power-down, the required level must be held long enough for the mcu to complete the wake-up to trigger the level interrupt. if the level disappears before the end of the start-up time, the mcu will still wake up, but no interrupt will be generated. the start-up time is defined as described in ?clock system? on page 17 . assembly code example .org 0x0000 ;set address of next statement rjmp reset ; address 0x0000 rjmp int0_isr ; address 0x0001 rjmp pcint0_isr ; address 0x0002 rjmp pcint1_isr ; address 0x0003 rjmp wdt_isr ; address 0x0004 rjmp tim1_capt_isr ; address 0x0005 rjmp tim1_compa_isr ; address 0x0006 rjmp tim1_compb_isr ; address 0x0007 rjmp tim1_ovf_isr ; address 0x0008 rjmp tim0_compa_isr ; address 0x0009 rjmp tim0_compb_isr ; address 0x000a rjmp tim0_ovf_isr ; address 0x000b rjmp ana_comp_isr ; address 0x000c rjmp adc_isr ; address 0x000d rjmp twi_slave_isr ; address 0x000e rjmp spi_isr ; address 0x000f rjmp qtrip_isr ; address 0x0010 reset: ; main program start ; address 0x0011 ...
38 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 if the low level on the interrupt pin is removed before the device has woken up then program execution will not be diverted to the interrupt service routine but continue from the instruction following the sleep command. 9.2.2 pin change interrupt timing a timing example of a pin change interrupt is shown in figure 9-1 . figure 9-1. timing of pin change interrupts 9.3 register description 9.3.1 mcucr ? mcu control register the mcu control register contains bits for controlling external interrupt sensing and power management. z bits 7:6 ? isc01, isc00: interrupt sense control the external interrupt 0 is activated by the external pin int0 if the sreg i-flag and the corresponding interrupt mask are set. the level and edges on the external int0 pin that activate the interrupt are defined in table 9-2 . the value on the int0 pin is sampled before detecting edges. if edge or toggle interrupt is selected, pulses that last longer than one clock period will generate an interrupt. shorter pulses are not guaranteed to generate an interrupt. if low level interrupt is selected, the low level must be held until the completion of the currently executing instruction to generate an interrupt. clk pcint(0) pin_lat pin_sync pcint_in_(0) pcint_syn p cint_setflag pcif pcint(0) pin_sync pcint_syn pin_lat d q le pcint_setflag pc if clk clk pcint(0) in pcmsk(x) pcint_in_(0) 0 x bit 76543210 0x3a isc01 isc00 ? bods sm2 sm1 sm0 se mcucr read/write r/w r/w r r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
39 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 table 9-2. interrupt 0 sense control 9.3.2 gimsk ? general interrupt mask register z bits 7:6 ? res: reserved bits these bits are reserved and will always read as zero. z bit 5 ? pcie1: pin change interrupt enable 1 when the pcie1 bit is set (one) and the i-bit in the status register (sreg) is set (one), pin change interrupt 1 is enabled. any change on any enabled pcint[11:8] pin will cause an interrupt. the corresponding interrupt of pin change interrupt request is executed from the pci1 interrupt vector. pcint[11:8] pins are enabled individually by the pcmsk1 register. z bit 4 ? pcie0: pin change interrupt enable 0 when the pcie0 bit is set (one) and the i-bit in the status register (sreg) is set (one), pin change interrupt 0 is enabled. any change on any enabled pcint[7:0] pin will cause an interrupt. the corresponding interrupt of pin change interrupt request is executed from the pci0 interrupt vector. pcint[7:0] pins are enabled individually by the pcmsk0 register. z bits 3:1 ? res: reserved bits these bits are reserved and will always read as zero. z bit 0 ? int0: external interrupt request 0 enable when the int0 bit is set (one) and the i-bit in the status register (sreg) is set (one), the external pin interrupt is enabled. the interrupt sense control bits (isc01 and isc00) in the mcu control register (mcucr) define whether the external interrupt is activated on rising and/or falling edge of the int0 pin or level sensed. activity on the pin will cause a n interrupt request even if int0 is configured as an output. the corresponding interrupt of external interrupt request 0 is executed from the int0 interrupt vector. isc01 isc00 description 0 0 the low level of int0 generates an interrupt request. 0 1 any logical change on int0 generates an interrupt request. 1 0 the falling edge of int0 generates an interrupt request. 1 1 the rising edge of int0 generates an interrupt request. bit 76543210 0x0c ? ? pcie1 pcie0 ? ? ? int0 gimsk read/write r r r/w r/w r r r r/w initial value 0 0000000
40 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 9.3.3 gifr ? general interrupt flag register z bits 7:6 ? res: reserved bits these bits are reserved and will always read as zero. z bit 5 ? pcif1: pin change interrupt flag 1 when a logic change on any pcint[11:8] pin triggers an interrupt request, pcif1 becomes set (one). if the i-bit in sreg and the pcie1 bit in gimsk are set (one), the mcu will jump to the corresponding interrupt vector. the flag is cleared when the interrupt routine is executed. alternatively, the flag can be cleared by writing a logical one to it. z bit 4 ? pcif0: pin change interrupt flag 0 when a logic change on any pcint[7:0] pin triggers an interrupt request, pcif becomes set (one). if the i-bit in sreg and the pcie0 bit in gimsk are set (one), the mcu will jump to the corresponding interrupt vector. the flag is cleared when the interrupt routine is executed. alternatively, the flag can be cleared by writing a logical one to it. z bits 3:1 ? res: reserved bits these bits are reserved and will always read as zero. z bit 0 ? intf0: external interrupt flag 0 when an edge or logic change on the int0 pin triggers an interrupt request, intf0 becomes set (one). if the i-bit in sreg and the int0 bit in gimsk are set (one), the mcu will jump to the corresponding interrupt vector. the flag is cleared when the interrupt routine is executed. alternatively, the flag can be cleared by writing a logical one to it. this fla g is always cleared when int0 is configured as a level interrupt. 9.3.4 pcmsk1 ? pin change mask register 1 z bits 7:4 ? res: reserved bits these bits are reserved and will always read as zero. z bits 3:0 ? pcint[11:8]: pin change enable mask 11:8 each pcint[11:8] bit selects whether pin change interrupt is enabled on the corresponding i/o pin. if pcint[11:8] is set and the pcie1 bit in gimsk is set, pin change interrupt is enabled on the corresponding i/o pin. if pcint[11:8] is cleared, pin change interrupt on the corresponding i/o pin is disabled. bit 76543210 0x0b ? ? pcif1 pcif0 ? ? ? intf0 gifr read/write r r r/w r/w r r r r/w initial value 0 0000000 bit 7 6 5 4 3 2 1 0 0x0a ? ? ? ? pcint11 pcint10 pcint9 pcint8 pcmsk1 read/write r r r r r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
41 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 9.3.5 pcmsk0 ? pin change mask register 0 z bits 7:0 ? pcint[7:0]: pin change enable mask 7:0 each pcint[7:0] bit selects whether pin change interrupt is enabled on the corresponding i/o pin. if pcint[7:0] is set and the pcie0 bit in gimsk is set, pin change interrupt is enabled on the corresponding i/o pin. if pcint[7:0] is cleared, pin change interrupt on the corresponding i/o pin is disabled. bit 76543210 0x09 pcint7 pcint6 pcint5 pcint4 pcint3 pcint2 pcint1 pcint0 pcmsk0 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
42 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 10. i/o ports 10.1 overview all avr ports have true read-modify-write functionality when used as general digital i/o ports. this means that the direction of one port pin can be changed without unintentionally changing the direction of any other pin with the sbi and cbi instructions. the same applies when changing drive value (if configured as output) or enabling/disabling of pull-up resistors. each output buffer has symmetrical drive characteristics with both high sink and source capability. the pin driver is strong enough to drive led displays directly. all port pins have individually selectable pull-up resistors with a supply-voltage invariant resistance. all i/o pins have protection diodes to both v cc and ground as indicated in figure 10- 1 on page 42 . see ?electrical characteristics? on page 167 for a complete list of parameters. figure 10-1. i/o pin equivalent schematic all registers and bit references in this section are written in general form. a lower case ?x? represents the numbering letter for the port, and a lower case ?n? represents the bit number. however, when using the register or bit defines in a program, the precise form must be used. for example, portb3 for bit no. 3 in port b, here documented generally as portxn. the physical i/o registers and bit locations are listed in ?register description? on page 56 . four i/o memory address locations are allocated for each port, one each for the data register ? portx, data direction register ? ddrx, pull-up enable register ? puex, and the port input pins ? pinx. the port input pins i/o location is read only, while the data register, the data direction register, and the pull-up enable register are read/write. however, writing a logic one to a bit in the pinx register, will result in a toggle in the corresponding bit in the data register. using the i/o port as general digital i/o is described in ?ports as general digital i/o? on page 42 . most port pins are multiplexed with alternate functions for the peripheral features on the device. how each alternate function interferes with the port pin is described in ?alternate port functions? on page 47 . refer to the individual module sections for a full description of the alternate functions. note that enabling the alternate function of some of the port pins does not affect the use of the other pins in the port as general digital i/o. 10.2 ports as general digital i/o the ports are bi-directional i/o ports with optional internal pull-ups. figure 10-2 shows a functional description of one i/o- port pin, here generically called pxn. c pin logic r pu see figure "general digital i/o" for details pxn
43 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 10-2. general digital i/o (1) note: 1. wex, wrx, wpx, wdx, rex, rrx, rpx, and rdx are common to all pins within the same port. clk i/o , and sleep are common to all ports. 10.2.1 configuring the pin each port pin consists of four register bits: ddxn, portxn, puexn, and pinxn. as shown in ?register description? on page 56 , the ddxn bits are accessed at the ddrx i/o address, the portxn bits at the portx i/o address, the puexn bits at the puex i/o address, and the pinxn bits at the pinx i/o address. the ddxn bit in the ddrx register selects the direction of this pin. if ddxn is written logic one, pxn is configured as an output pin. if ddxn is written logic zero, pxn is configured as an input pin. if portxn is written logic one when the pin is configured as an output pin, the port pin is driven high (one). if portxn is written logic zero when the pin is configured as an output pin, the port pin is driven low (zero). clk rpx rrx rdx wdx wex synchronizer wdx: write ddrx wrx: write portx rrx: read portx register rpx: read portx pin clk i/o : i/o clock rdx: read ddrx wex: write puex rex: read puex d l q q rex reset reset q q d q qd clr portxn q qd clr ddxn pinxn data bus sleep sleep: sleep control pxn i/o wpx reset q qd clr puexn 0 1 wrx wpx: write pinx register
44 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 the pull-up resistor is activated, if the puexn is written logic one. to switch the pull-up resistor off, puexn has to be written logic zero. table 10-1 summarizes the control signals for the pin value. table 10-1. port pin configurations port pins are tri-stated when a reset condition becomes active, even when no clocks are running. 10.2.2 toggling the pin writing a logic one to pinxn toggles the value of portxn, independent on the value of ddrxn. note that the sbi instruction can be used to toggle one single bit in a port. 10.2.3 break-before-make switching in break-before-make mode, switching the ddrxn bit from input to output introduces an immediate tri-state period lasting one system clock cycle, as indicated in figure 10-3 . for example, if the system clock is 4 mhz and the ddrxn is written to make an output, an immediate tri-state period of 250 ns is introduced before the value of portxn is seen on the port pin. to avoid glitches it is recommended that the maximum ddrxn toggle frequency is two system clock cycles. the break- before-make mode applies to the entire port and it is activated by the bbmx bit. for more details, see ?portcr ? port control register? on page 56 . when switching the ddrxn bit from output to input no immediate tri-state period is introduced. ddxn portxn puexn i/o pull-up comment 0 x 0 input no tri-state (hi-z) 0 x 1 input yes sources current if pulled low externally 1 0 0 output no output low (sink) 1 0 1 output yes not recommended. output low (sink) and internal pull-up active. sources current through the internal pull-up resistor and consumes power constantly 1 1 0 output no output high (source) 1 1 1 output yes output high (source) and internal pull-up active
45 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 10-3. switching between input and output in break-before-make-mode 10.2.4 reading the pin value independent of the setting of data direction bit ddxn, the port pin can be read through the pinxn register bit. as shown in figure 10-2 on page 43 , the pinxn register bit and the preceding latch constitute a synchronizer. this is needed to avoid metastability if the physical pin changes value near the edge of the internal clock, but it also introduces a delay. figure 10-4 shows a timing diagram of the synchronization when reading an externally applied pin value. the maximum and minimum propagation delays are denoted t pd,max and t pd,min respectively. figure 10-4. synchronization when reading an externally applied pin value consider the clock period starting shortly after the first falling edge of the system clock. the latch is closed when the clock is low, and goes transparent when the clock is high, as indicated by the shaded region of the ?sync latch? signal. the signal value is latched when the system clock goes low. it is clocked into the pinxn register at the succeeding positive clock edge. as indicated by the two arrows tpd,max and tpd,min, a single signal transition on the pin will be delayed between ? and 1? system clock period depending upon the time of assertion. out ddrx, r16 nop 0x02 0x01 system clk instructions ddrx intermediate tri-state cycle out ddrx, r17 0x55 portx 0x01 intermediate tri-state cycle px0 px1 tri-state tri-state tri-state 0x01 r17 0x02 r16 xxx in r17, pinx 0x00 0xff in s truction s s ync latch pinxn r17 xxx s y s tem clk t pd, m a x t pd, min
46 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 when reading back a software assigned pin value, a nop instruction must be inserted as indicated in figure 10-5 on page 46 . the out instruction sets the ?sync latch? signal at the positive edge of the clock. in this case, the delay tpd through the synchronizer is one system clock period. figure 10-5. synchronization when reading a software assigned pin value 10.2.5 digital input enable and sleep modes as shown in figure 10-2 on page 43 , the digital input signal can be clamped to ground at the input of the schmitt-trigger. the signal denoted sleep in the figure, is set by the mcu sleep controller in power-down and standby modes to avoid high power consumption if some input signals are left floating, or have an analog signal level close to v cc /2. sleep is overridden for port pins enabled as external interrupt pins. if the external interrupt request is not enabled, sleep is active also for these pins. sleep is also overridden by various other alternate functions as described in ?alternate port functions? on page 47 . if a logic high level (?one?) is present on an asynchronous external interrupt pin configured as ?interrupt on rising edge, falling edge, or any logic change on pin? while the external interrupt is not enabled, the corresponding external interrupt flag will be set when resuming from the above mentioned sleep mode, as the clamping in these sleep mode produces the requested logic change. 10.2.6 unconnected pins if some pins are unused, it is recommended to ensure that these pins have a defined level. even though most of the digital inputs are disabled in the deep sleep modes as described above, floating inputs should be avoided to reduce current consumption in all other modes where the digital inputs are enabled (reset, active mode and idle mode). the simplest method to ensure a defined level of an unused pin, is to enable the internal pull-up. in this case, the pull-up will be disabled during reset. if low power consumption during reset is important, it is recommended to use an external pull-up or pull-down. connecting unused pins directly to v cc or gnd is not recommended, since this may cause excessive currents if the pin is accidentally configured as an output. out portx, r16 nop in r17, pinx 0xff 0x00 0xff system clk r16 instructions sync latch pinxn r17 t pd
47 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 10.2.7 program example the following code example shows how to set port b pin 0 high, pin 1 low, and define the port pins from 2 to 3 as input with a pull-up assigned to port pin 2. the resulting pin values are read back again, but as previously discussed, a nop instruction is included to be able to read back the value recently assigned to some of the pins. note: see ?code examples? on page 7 . 10.3 alternate port functions most port pins have alternate functions in addition to being general digital i/os. in figure 10-6 below is shown how the port pin control signals from the simplified figure 10-2 on page 43 can be overridden by alternate functions. assembly code example ... ; define pull-ups and set outputs high ; define directions for port pins ldi r16,(1< 48 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 10-6. alternate port functions (1) note: 1. wex, wrx, wpx, wdx, rex, rrx, rpx, and rdx are common to all pins within the same port. clk i/o , and sleep are common to all ports. all other signals are unique for each pin. clk rpx rrx wrx rdx wdx synchronizer wdx: write ddrx wrx: write portx rrx: read portx register rpx: read portx pin rex: read puex wex: write puex clk i/o : i/o clock rdx: read ddrx d l q q set clr 0 1 0 1 0 1 dixn aioxn dieoexn pvovxn pvoexn ddovxn ddoexn puoexn puovxn puoexn: pxn pull-up override enable puovxn: pxn pull-up override value ddoexn: pxn data direction override enable ddovxn: pxn data direction override value pvoexn: pxn port value override enable pvovxn: pxn port value override value dixn: digital input pin n on portx aioxn: analog input/output pin n on portx reset reset q qd clr q qd clr q q d clr pinxn portxn ddxn data bus 0 1 dieovxn sleep dieoexn: pxn digital input-enable override enable dieovxn: pxn digital input-enable override value sleep: sleep control pxn i/o 0 1 ptoexn ptoexn: pxn, port toggle override enable wpx: write pinx wpx wex rex reset q qd clr puexn
49 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 the illustration in the figure above serves as a generic description applicable to all port pins in the avr microcontroller family. some overriding signals may not be present in all port pins. table 10-2 summarizes the function of the overriding signals. the pin and port indexes from figure 10-6 on page 48 are not shown in the succeeding tables. the overriding signals are generated internally in the modules having the alternate function. table 10-2. generic description of overriding signals for alternate functions the following subsections shortly describe the alternate functions for each port, and relate the overriding signals to the alternate function. refer to the alternate function description for further details. 10.3.1 alternate functions of port a the port a pins with alternate function are shown in table 10-3 . signal name full name description puoe pull-up override enable if this signal is set, the pull-up enable is controlled by the puov signal. if this signal is cleared, the pull-up is enabled when puexn = 0b1. puov pull-up override value if puoe is set, the pull-up is enabled/disabled when puov is set/cleared, regardless of the setting of the puexn register bit. ddoe data direction override enable if this signal is set, the output driver enable is controlled by the ddov signal. if this signal is cleared, the output driver is enabled by the ddxn register bit. ddov data direction override value if ddoe is set, the output driver is enabled/disabled when ddov is set/cleared, regardless of the setting of the ddxn register bit. pvoe port value override enable if this signal is set and the output driver is enabled, the port value is controlled by the pvov signal. if pvoe is cleared, and the output driver is enabled, the port value is controlled by the portxn register bit. pvov port value override value if pvoe is set, the port value is set to pvov, regardless of the setting of the portxn register bit. ptoe port toggle override enable if ptoe is set, the portxn register bit is inverted. dieoe digital input enable override enable if this bit is set, the digital input enable is controlled by the dieov signal. if this signal is cleared, the digital input enable is determined by mcu state (normal mode, sleep mode). dieov digital input enable override value if dieoe is set, the digital input is enabled/disabled when dieov is set/cleared, regardless of the mcu state (normal mode, sleep mode). di digital input this is the digital input to alternate functions. in the figure, the signal is connected to the output of the schmitt-trigger but before the synchronizer. unless the digital input is used as a clock source, the module with the alternate function will use its own synchronizer. aio analog input/output this is the analog input/output to/from alternate functions. the signal is connected directly to the pad, and can be used bi-directionally.
50 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 table 10-3. port a pins alternate functions z port a, bit 0 ? adc0/pcint0 z adc0: analog to digital converter, channel 0 . z pcint0: pin change interrupt source 0. the pa0 pin can serve as an external interrupt source for pin change interrupt 0. z port a, bit 1 ? adc1/ain0/pcint1 z adc1: analog to digital converter, channel 1 . z ain0: analog comparator positive input. configure the port pin as input with the internal pull-up switched off to avoid the digital port function from interfering with the function of the analog comparator. z pcint1: pin change interrupt source 1. the pa1 pin can serve as an external interrupt source for pin change interrupt 0. z port a, bit 2 ? adc2/ain1/pcint2 z adc2: analog to digital converter, channel 2 . z ain1: analog comparator negative input. configure the port pin as input with the internal pull-up switched off to avoid the digital port function from interfering with the function of the analog comparator. port pin alternate function pa0 adc0: adc input channel 0 pcint0: pin change interrupt 0, source 0 pa1 adc1: adc input channel 1 ain0: analog comparator, positive input pcint1:pin change interrupt 0, source 1 pa2 adc2: adc input channel 2 ain1: analog comparator, negative input pcint2: pin change interrupt 0, source 2 pa3 adc3: adc input channel 3 pcint3: pin change interrupt 0, source 3 pa4 adc4: adc input channel 4 pcint4: pin change interrupt 0, source 4 pa5 adc5: adc input channel 5 pcint5: pin change interrupt 0, source 5 pa6 adc6: adc input channel 6 ss : spi slave select pcint6: pin change interrupt 0, source 6 pa7 adc7: adc input channel 7 oc0b:: timer/counter0 compare match b output icp1: timer/counter1 input capture pin t1: timer/counter1 clock source scl: twi clock sck: spi clock pcint7: pin change interrupt 0, source 7
51 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 z pcint2: pin change interrupt source 2. the pa2 pin can serve as an external interrupt source for pin change interrupt 0. z port a, bit 3 ? adc3/pcint3 z adc3: analog to digital converter, channel 3 . z pcint3: pin change interrupt source 3. the pa3 pin can serve as an external interrupt source for pin change interrupt 0. z port a, bit 4 ? adc4/pcint4 z adc4: analog to digital converter, channel 4 . z pcint4: pin change interrupt source 4. the pa4 pin can serve as an external interrupt source for pin change interrupt 0. z port a, bit 5 ? adc5/pcint5 z adc5: analog to digital converter, channel 5 . z pcint5: pin change interrupt source 5. the pa5 pin can serve as an external interrupt source for pin change interrupt 0. z port a, bit 6 ? adc6/ ss/pcint6 z adc6: analog to digital converter, channel 6 . z ss: slave select input. regardless of dda6, this pin is automatically configured as an input when spi is enabled as a slave. the data direction of this pin is controlled by dda6 when spi is enabled as a master. z pcint6: pin change interrupt source 6. the pa6 pin can serve as an external interrupt source for pin change interrupt 0. z port a, bit 7 ? adc7/oc0b/icp1/t1/scl/sck/pcint7 z adc7: analog to digital converter, channel 7 . z oc0b: output compare match output. the pa7 pin can serve as an external output for the timer/counter0 compare match b. the pin has to be configured as an output (dda7 set (one)) to serve this function. this is also the output pin for the pwm mode timer function. z icp1: input capture pin. the pa7 pin can act as an input capture pin for timer/counter1. z t1: timer/counter1 counter source. z scl: twi clock. the pin is disconnected from the port and becomes the serial clock for the twi when twen in twscra is set. in this mode of operation, the pin is driven by an open drain driver with slew rate limitation and a spike filter. z sck: spi master clock output / slave clock input. regardless of dda7, this pin is automatically configured as an input when spi is enabled as a slave. the data direction of the pin is controlled by dda7 when spi is enabled as a master. z pcint7: pin change interrupt source 7. the pa7 pin can serve as an external interrupt source for pin change interrupt 0. table 10-4 and table 10-5 relate the alternate functions of port a to the overriding signals shown in figure 10-6 on page 48 .
52 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 table 10-4. overriding signals for alternate functions in pa[7:5] note: when twi is enabled the slew rate control and spike filter are activated on pa7. this is not illustrated in figure 10-6 on page 48 . the spike filter is connected between aioxn and the twi module. table 10-5. overriding signals for alternate functions in pa[4:2] signal name pa7/adc7/oc0b/icp1/ t1/scl/sck/pcint7 pa6/adc6/ ss/pcint6 pa5/adc5/pcint5 puoe 0 0 0 puov 0 0 0 ddoe twen + (spe ? mstr) spe ? mstr 0 ddov twen ? scl_out 0 0 pvoe twen + (spe ? mstr) + oc0b_enable 0 0 pvov twen ? (spe ? mstr ? sck_out + ( spe + mstr) ? oc0b) 0 0 ptoe 0 0 0 dieoe pcint7 ? pcie0 + adc7d pcint6 ? pcie0 + adc6d pcint5 ? pcie0 + adc5d dieov pcint7 ? pcie0 pcint6 ? pcie0 pcint5 ? pcie0 di icp1 / sck / t1 / scl / pcint7 input spi ss / pcint6 input pcint5 input aio adc7 / scl input adc6 input adc5 input signal name pa4/adc4/pcint4 pa3/adc3/pcint3 pa2/adc2/ain1/pcint2 puoe 0 0 0 puov 0 0 0 ddoe 0 0 0 ddov 0 0 0 pvoe 0 0 0 pvov 0 0 0 ptoe 0 0 0 dieoe (pcint4 ? pcie0) + adc4d (pcint3 ? pcie0) + adc3d (pcint2 ? pcie0) + adc2d dieov (pcint4 ? pcie0) pcint3 ? pcie0 pcint3 ? pcie0 di pcint4 input pcint1 input pcint0 input aio adc4 input adc3 input adc2 / analog comparator negative input
53 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 table 10-6. overriding signals for alternate functions in pa[1:0] 10.3.2 alternate functions of port b the port b pins with alternate function are shown in table 10-7 . table 10-7. port b pins alternate functions signal name pa1/adc1/ain0/pcint1 pa0/adc0/pcint0 puoe 0 0 puov 0 0 ddoe 0 0 ddov 0 0 pvoe 0 0 pvov 0 0 ptoe 0 0 dieoe pcint1 ? pcie0 + adc1d pcint0 ? pcie0 + adc0d dieov pcint1 ? pcie0 pcint0 ? pcie0 di pcint1 input pcint0 input aio adc1 / analog comparator positive input adc1 input port pin alternate function pb0 t0: timer/counter0 clock source clki: external clock input tpiclk: serial programming clock pcint8: pin change interrupt 1, source 8 pb1 oc1a: timer/counter1 compare match a output sda: twi data input/output mosi: spi master output / slave input tpidata: serial programming data pcint9: pin change interrupt 1, source 9 pb2 int0: external interrupt 0 input oc0a: timer/counter0 compare match a output oc1b: timer/counter1 compare match b output miso: spi master input / slave output ckout: system clock output pcint10:pin change interrupt 1, source 10 pb3 reset: reset pin pcint11:pin change interrupt 1, source 11.
54 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 z port b, bit 0 ? t0/clki/tpiclk/pcint8 z t0: timer/counter0 clock source. z clki: clock input from an external clock source, see ?external clock? on page 18 . z tpiclk: serial programming clock. z pcint8: pin change interrupt source 8. the pb0 pin can serve as an external interrupt source for pin change interrupt 1. z port b, bit 1 ? oc1a/sda/mosi/tpidata/pcint9 z oc1a: output compare match output. provided that the pin has been configured as an output it serves as an external output for timer/counter1 compare match a. this pin is also the output for the timer/counter pwm mode function. z sda: twi data. the pin is disconnected from the port and becomes the serial data for the twi when twen in twscra is set. in this mode of operation, the pin is driven by an open drain driver with slew rate limitation and a spike filter. z mosi: spi master output / slave input. regardless of ddb1, this pin is automatically configured as an input when spi is enabled as a slave. the data direction of this pin is controlled by ddb1 when spi is enabled as a master. z tpidata: serial programming data. z pcint9: pin change interrupt source 9. the pb1 pin can serve as an external interrupt source for pin change interrupt 1. z port b, bit 2 ? int0/oc0a/oc1b/miso/ckout/pcint10 z int0: external interrupt request 0. z oc0a: output compare match output. provided that the pin has been configured as an output it serves as an external output for timer/counter0 compare match a. this pin is also the output for the timer/counter pwm mode function. z oc1b: output compare match output. provided that the pin has been configured as an output it serves as an external output for timer/counter1 compare match b. this pin is also the output for the timer/counter pwm mode function. z miso: spi master input / slave output. regardless of ddb2, this pin is automatically configured as an input when spi is enabled as a master. the data direction of this pin is controlled by ddb2 when spi is enabled as a slave. z ckout - system clock output: the system clock can be output on the pb2 pin. the system clock will be output if the ckout fuse is programmed, regardless of the portb2 and ddb2 settings. it will also be output during reset. z pcint10: pin change interrupt source 10. the pb2 pin can serve as an external interrupt source for pin change interrupt 1. z port b, bit 3 ? reset/pcint11 z reset: external reset input is active low and enabled by unprogramming (?1?) the rstdisbl fuse. pull- up is activated and output driver and digital input are deactivated when the pin is used as the reset pin. z pcint11: pin change interrupt source 11. the pb3 pin can serve as an external interrupt source for pin change interrupt 1. table 10-8 and table 10-9 relate the alternate functions of port b to the overriding signals shown in figure 10-6 on page 48 .
55 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 table 10-8. overriding signals for alternate functions in pb[3:2] signal pb3/ reset/ pcint11 pb2/int0/oc0a/oc1b/miso/ckout/pcint10 puoe rstdisbl (1) 1. rstdisbl is 1 when the configuration bit is ?0? (programmed) ckout (2) 2. ckout is 1 when the configuration bit is ?0? (programmed) puov 1 0 ddoe rstdisbl (1) ckout (2) + (spe ? mstr) ddov 0 ckout (2) pvoe rstdisbl (1) ckout + oc0a_enable + oc1b_enable + (spe ? mstr) pvov 0 ckout (2) ? system clock + ckout ? spe ? mstr ? spi_slave_out + ckout ? ( spe + mstr) ? oc1b_enable ? oc1b + ckout ? ( spe + mstr) ? oc1b_enable ? oc0a ptoe 0 0 dieoe rstdisbl (1) + (pcint11 ? pcie1) (pcint10 ? pcie1) + int0 dieov rstdisbl (1) ? pcint11 ? pcie1 (pcint10 ? pcie1) + int0 di pcint11 input int0 / pcint10 / spi master input aio
56 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 table 10-9. overriding signals for alternate functions in pb[1:0] note: when twi is enabled the slew rate control and spike filter are activate on pb1. this is not illustrated in figure 10-6 on page 48 . the spike filter is connected between aioxn and the twi. 10.4 register description 10.4.1 portcr ? port control register z bits 7:2 ? res: reserved bits these bits are reserved and will always read as zero. z bit 1 ? bbmb: break-before-make mode enable when this bit is set the break-before-make mode is activated for the entire port b. the intermediate tri-state cycle is then inserted when writing ddrbn to make an output. for further information, see ?break-before-make switching? on page 44 . z bit 0 ? bbma: break-before-make mode enable when this bit is set the break-before-make mode is activated for the entire port a. the intermediate tri-state cycle is then inserted when writing ddran to make an output. for further information, see ?break-before-make switching? on page 44 . signal pb1/oc1a/sda/mosi/pcint9 pb0/t0/clki/pcint8 puoe 0 ext_clock (1) 1. ext_clock = external clock is selected as system clock. puov 0 0 ddoe (spe ? mstr) + twen ext_clock (1) ddov twen ? sda_out 0 pvoe twen + (spe ? mstr) + oc1a_enable ext_clock (1) pvov twen ? spe ? mstr ? spi_master_out + twen ? ( spe + mstr) ? oc1a 0 ptoe 0 0 dieoe pcint9 ? pcie1 ext_clock (1) + (pcint8 ? pcie1) dieov pcint9 ? pcie1 ( ext_clock (1) ? pwr_down ) + ( ext_clock (1) ? pcint8 ? pcie1) di pcint9 / spi slave input clock / pcint8 / t0 input aio sda input bit 7 6 5 4 3 2 1 0 0x08 ? ? ? ? ? ? bbmb bbma portcr read/write r r r r r r r/w r/w initial value 0 0 0 0 0 0 0 0
57 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 10.4.2 puea ? port a pull-up enable control register 10.4.3 porta ? port a data register 10.4.4 ddra ? port a data direction register 10.4.5 pina ? port a input pins 10.4.6 pueb ? port b pull-up enable control register 10.4.7 portb ? port b data register 10.4.8 ddrb ? port b data direction register bit 76543210 0x03 puea7 puea6 puea5 puea4 puea3 puea2 puea1 puea0 puea read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 00000 bit 76543210 0x02 porta7 porta6 porta5 porta4 porta3 porta2 porta1 porta0 porta read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 00000 bit 76543210 0x01 dda7 dda6 dda5 dda4 dda3 dda2 dda1 dda0 ddra read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 00000 bit 76543210 0x00 pina7 pina6 pina5 pina4 pina3 pina2 pina1 pina0 pina read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value n/a n/a n/a n/a n/a n/a n/a n/a bit 76543210 0x07 ? ? ? ? pueb3 pueb2 pueb1 pueb0 pueb read/write r r r r r/w r/w r/w r/w initial value 0 0 0 00000 bit 76543210 0x06 ? ? ? ? portb3 portb2 portb1 portb0 portb read/write r r r r r/w r/w r/w r/w initial value 0 0 0 00000 bit 76543210 0x05 ? ? ? ? ddb3 ddb2 ddb1 ddb0 ddrb read/write r r r r r/w r/w r/w r/w initial value 0 0 0 00000
58 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 10.4.9 pinb ? port b input pins bit 76543210 0x04 ? ? ? ? pinb3 pinb2 pinb1 pinb0 pinb read/write r r r r r/w r/w r/w r/w initial value 0 0 0 0 n/a n/a n/a n/a
59 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 11. 8-bit timer/counter0 with pwm 11.1 features z two independent output compare units z double buffered output compare registers z clear timer on compare match (auto reload) z glitch free, phase correct pulse width modulator (pwm) z variable pwm period z frequency generator z three independent interrupt sources (tov0, ocf0a, and ocf0b) 11.2 overview timer/counter0 is a general purpose 8-bit timer/counter module, with two independent output compare units, and with pwm support. it allows accurate program execution timing (event management) and wave generation. figure 11-1. 8-bit timer/counter block diagram clock select timer/counter data bus ocrna ocrnb = = tcntn waveform generation waveform generation ocna ocnb = fixed top value control logic = 0 top bottom count clear direction tovn (int.req.) ocna (int.req.) ocnb (int.req.) tccrna tccrnb tn edge detector ( from prescaler ) clk tn
60 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 a simplified block diagram of the 8-bit timer/counter is shown in figure 11-1 on page 59 . for the actual placement of i/o pins, refer to figure 1-1 on page 2 . cpu accessible i/o registers, including i/o bits and i/o pins, are shown in bold. the device-specific i/o register and bit locations are listed in the ?register description? on page 69 . 11.2.1 registers the timer/counter (tcnt0) and output compare registers (ocr0a and ocr0b) are 8-bit registers. interrupt request (abbreviated to int.req. in figure 11-1 ) signals are all visible in the timer interrupt flag register (tifr). all interrupts are individually masked with the timer interrupt mask register (timsk). tifr and timsk are not shown in the figure. the timer/counter can be clocked internally, via the prescaler, or by an external clock source on the t0 pin. the clock select logic block controls which clock source and edge the timer/counter uses to increment (or decrement) its value. the timer/counter is inactive when no clock source is selected. the output from the clock select logic is referred to as the timer clock (clk t0 ). the double buffered output compare registers (ocr0a and ocr0b) is compared with the timer/counter value at all times. the result of the compare can be used by the waveform generator to generate a pwm or variable frequency output on the output compare pins (oc0a and oc0b). see ?output compare unit? on page 61 for details. the compare match event will also set the compare flag (ocf0a or ocf0b) which can be used to generate an output compare interrupt request. 11.2.2 definitions many register and bit references in this section are written in general form. a lower case ?n? replaces the timer/counter number, in this case 0. a lower case ?x? replaces the output compare unit, in this case compare unit a or compare unit b. however, when using the register or bit defines in a program, the precise form must be used, i.e., tcnt0 for accessing timer/counter0 counter value and so on. the definitions in table 11-1 are also used extensively throughout the document. table 11-1. definitions 11.3 clock sources the timer/counter can be clocked by an internal or an external clock source. the clock source is selected by the clock select logic which is controlled by the clock select (cs0[2:0]) bits located in the timer/counter control register (tccr0b). for details on clock sources and prescaler, see ?timer/counter prescaler? on page 103 . 11.4 counter unit the main part of the 8-bit timer/counter is the programmable bi-directional counter unit. figure 11-2 on page 61 shows a block diagram of the counter and its surroundings. constant description bottom the counter reaches bottom when it becomes 0x00 max the counter reaches its maximum when it becomes 0xff (decimal 255) top the counter reaches the top when it becomes equal to the highest value in the count sequence. the top value can be assigned to be the fixed value 0xff (max) or the value stored in the ocr0a register. the assignment depends on the mode of operation
61 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 11-2. counter unit block diagram signal description (internal signals): count increment or decrement tcnt0 by 1. direction select between increment and decrement. clear clear tcnt0 (set all bits to zero). clk t n timer/counter clock, referred to as clk t0 in the following. top signalize that tcnt0 has reached maximum value. bottom signalize that tcnt0 has reached minimum value (zero). depending of the mode of operation used, the counter is cleared, incremented, or decremented at each timer clock (clk t0 ). clk t0 can be generated from an external or internal clock source, selected by the clock select bits (cs0[2:0]). when no clock source is selected (cs0[2:0] = 0) the timer is stopped. however, the tcnt0 value can be accessed by the cpu, regardless of whether clk t0 is present or not. a cpu write overrides (has priority over) all counter clear or count operations. the counting sequence is determined by the setting of the wgm01 and wgm00 bits located in the timer/counter control register (tccr0a) and the wgm02 bit located in the timer/counter control register b (tccr0b). there are close connections between how the counter behaves (counts) and how waveforms are generated on the output compare output oc0a. for more details about advanced counting sequences and waveform generation, see ?modes of operation? on page 64 . the timer/counter overflow flag (tov0) is set according to the mode of operation selected by the wgm0[1:0] bits. tov0 can be used for generating a cpu interrupt. 11.5 output compare unit the 8-bit comparator continuously compares tcnt0 with the output compare registers (ocr0a and ocr0b). whenever tcnt0 equals ocr0a or ocr0b, the comparator signals a match. a match will set the output compare flag (ocf0a or ocf0b) at the next timer clock cycle. if the corresponding interrupt is enabled, the output compare flag generates an output compare interrupt. the output compare flag is automatically cleared when the interrupt is executed. alternatively, the flag can be cleared by software by writing a logical one to its i/o bit location. the waveform generator uses the match signal to generate an output according to operating mode set by the wgm0[2:0] bits and compare output mode (com0x[1:0]) bits. the max and bottom signals are used by the waveform generator for handling the special cases of the extreme values in some modes of operation. see ?modes of operation? on page 64 . figure 11-3 on page 62 shows a block diagram of the output compare unit. data b u s tcntn control logic count tovn (int.req.) clock select top tn edge detector ( from prescaler ) clk tn bottom direction clear
62 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 11-3. output compare unit, block diagram the ocr0x registers are double buffered when using any of the pulse width modulation (pwm) modes. for the normal and clear timer on compare (ctc) modes of operation, the double buffering is disabled. the double buffering synchronizes the update of the ocr0x compare registers to either top or bottom of the counting sequence. the synchronization prevents the occurrence of odd-length, non-symmetrical pwm pulses, thereby making the output glitch- free. the ocr0x register access may seem complex, but this is not case. when the double buffering is enabled, the cpu has access to the ocr0x buffer register, and if double buffering is disabled the cpu will access the ocr0x directly. 11.5.1 force output compare in non-pwm waveform generation modes, the match output of the comparator can be forced by writing a one to the force output compare (0x) bit. forcing compare match will not set the ocf0x flag or reload/clear the timer, but the oc0x pin will be updated as if a real compare match had occurred (the com0x[1:0] bits settings define whether the oc0x pin is set, cleared or toggled). 11.5.2 compare match blocking by tcnt0 write all cpu write operations to the tcnt0 register will block any compare match that occur in the next timer clock cycle, even when the timer is stopped. this feature allows ocr0x to be initialized to the same value as tcnt0 without triggering an interrupt when the timer/counter clock is enabled. 11.5.3 using the output compare unit since writing tcnt0 in any mode of operation will block all compare matches for one timer clock cycle, there are risks involved when changing tcnt0 when using the output compare unit, independently of whether the timer/counter is running or not. if the value written to tcnt0 equals the ocr0x value, the compare match will be missed, resulting in incorrect waveform generation. similarly, do not write the tcnt0 value equal to bottom when the counter is down- counting. the setup of the oc0x should be performed before setting the data direction register for the port pin to output. the easiest way of setting the oc0x value is to use the force output compare (0x) strobe bits in normal mode. the oc0x registers keep their values even when changing between waveform generation modes. ocfn x (int.req.) 8-bit comparator ocrnx ocnx data b u s tcntn wgmn[2:0] waveform generator top focn comnx[1:0] bottom
63 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 be aware that the com0x[1:0] bits are not double buffered together with the compare value. changing the com0x[1:0] bits will take effect immediately. 11.6 compare match output unit the compare output mode (com0x[1:0]) bits have two functions. the waveform generator uses the com0x[1:0] bits for defining the output compare (oc0x) state at the next compare match. also, the com0x[1:0] bits control the oc0x pin output source. figure 11-4 on page 63 shows a simplified schematic of the logic affected by the com0x[1:0] bit setting. the i/o registers, i/o bits, and i/o pins in the figure are shown in bold. only the parts of the general i/o port control registers (ddr and port) that are affected by the com0x[1:0] bits are shown. when referring to the oc0x state, the reference is for the internal oc0x register, not the oc0x pin. if a system reset occur, the oc0x register is reset to ?0?. figure 11-4. compare match output unit, schematic the general i/o port function is overridden by the output compare (oc0x) from the waveform generator if either of the com0x[1:0] bits are set. however, the oc0x pin direction (input or output) is still controlled by the data direction register (ddr) for the port pin. the data direction register bit for the oc0x pin (ddr_oc0x) must be set as output before the oc0x value is visible on the pin. the port override function is independent of the waveform generation mode. the design of the output compare pin logic allows initialization of the oc0x state before the output is enabled. note that some com0x[1:0] bit settings are reserved for certain modes of operation, see ?register description? on page 69 11.6.1 compare output mode and waveform generation the waveform generator uses the com0x[1:0] bits differently in normal, ctc, and pwm modes. for all modes, setting the com0x[1:0] = 0 tells the waveform generator that no action on the oc0x register is to be performed on the next compare match. for compare output actions in the non-pwm modes refer to table 11-2 on page 70 . for fast pwm mode, refer to table 11-3 on page 70 , and for phase correct pwm refer to table 11-4 on page 70 . port ddr dq dq ocn pin ocnx dq waveform generator comnx1 comnx0 0 1 data b u s focn clk i/o
64 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 a change of the com0x[1:0] bits state will have effect at the first compare match after the bits are written. for non-pwm modes, the action can be forced to have immediate effect by using the force output compare bits. see ?tccr0b ? timer/counter control register b? on page 72 . 11.7 modes of operation the mode of operation, i.e., the behavior of the timer/counter and the output compare pins, is defined by the combination of the waveform generation mode (wgm[2:0]) and compare output mode (com0x[1:0]) bits. the compare output mode bits do not affect the counting sequence, while the waveform generation mode bits do. the com0x[1:0] bits control whether the pwm output generated should be inverted or not (inverted or non-inverted pwm). for non-pwm modes the com0x[1:0] bits control whether the output should be set, cleared, or toggled at a compare match (see ?modes of operation? on page 64 ). for detailed timing information refer to figure 11-8 on page 68 , figure 11-9 on page 68 , figure 11-10 on page 69 and figure 11-11 on page 69 in ?timer/counter timing diagrams? on page 68 . 11.7.1 normal mode the simplest mode of operation is the normal mode (wgm0[2:0] = 0). in this mode the counting direction is always up (incrementing), and no counter clear is performed. the counter simply overruns when it passes its maximum 8-bit value (top = 0xff) and then restarts from the bottom (0x00). in normal operation the timer/counter overflow flag (tov0) will be set in the same timer clock cycle as the tcnt0 becomes zero. the tov0 flag in this case behaves like a ninth bit, except that it is only set, not cleared. however, combined with the timer overflow interrupt that automatically clears the tov0 flag, the timer resolution can be increased by software. there are no special cases to consider in the normal mode, a new counter value can be written anytime. the output compare unit can be used to generate interrupts at some given time. using the output compare to generate waveforms in normal mode is not recommended, since this will occupy too much of the cpu time. 11.7.2 clear timer on compare match (ctc) mode in clear timer on compare or ctc mode (wgm0[2:0] = 2), the ocr0a register is used to manipulate the counter resolution. in ctc mode the counter is cleared to zero when the counter value (tcnt0) matches the ocr0a. the ocr0a defines the top value for the counter, hence also its resolution. this mode allows greater control of the compare match output frequency. it also simplifies the operation of counting external events. the timing diagram for the ctc mode is shown in figure 11-5 on page 64 . the counter value (tcnt0) increases until a compare match occurs between tcnt0 and ocr0a, and then counter (tcnt0) is cleared. figure 11-5. ctc mode, timing diagram t cntn o cna ( toggle) ocnx interrupt flag set 1 4 p eriod 2 3 (comna[1:0] = 1)
65 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 an interrupt can be generated each time the counter value reaches the top value by using the ocf0a flag. if the interrupt is enabled, the interrupt handler routine can be used for updating the top value. however, changing top to a value close to bottom when the counter is running with none or a low prescaler value must be done with care since the ctc mode does not have the double buffering feature. if the new value written to ocr0a is lower than the current value of tcnt0, the counter will miss the compare match. the counter will then have to count to its maximum value (0xff) and wrap around starting at 0x00 before the compare match can occur. for generating a waveform output in ctc mode, the oc0a output can be set to toggle its logical level on each compare match by setting the compare output mode bits to toggle mode (com0a[1:0] = 1). the oc0a value will not be visible on the port pin unless the data direction for the pin is set to output. when ocr0a is set to zero (0x00) the waveform generated will have a maximum frequency of f clk_i/o /2. the waveform frequency is defined by the following equation: the n variable represents the prescale factor (1, 8, 64, 256, or 1024). as for the normal mode of operation, the tov0 flag is set in the same timer clock cycle that the counter counts from max to 0x00. 11.7.3 fast pwm mode the fast pulse width modulation or fast pwm mode (wgm0[2:0] = 3 or 7) provides a high frequency pwm waveform generation option. the fast pwm differs from the other pwm option by its single-slope operation. the counter counts from bottom to top then restarts from bottom. top is defined as 0xff when wgm0[2:0] = 3, and ocr0a when wgm0[2:0] = 7. in non-inverting compare output mode, the output compare (oc0x) is cleared on the compare match between tcnt0 and ocr0x, and set at bottom. in inverting compare output mode, the output is set on compare match and cleared at bottom. due to the single-slope operation, the operating frequency of the fast pwm mode can be twice as high as the phase correct pwm mode that use dual-slope operation. this high frequency makes the fast pwm mode well suited for power regulation, rectification, and dac applications. high frequency allows physically small sized external components (coils, capacitors), and therefore reduces total system cost. in fast pwm mode, the counter is incremented until the counter value matches the top value. the counter is then cleared at the following timer clock cycle. the timing diagram for the fast pwm mode is shown in figure 11-6 on page 66 . the tcnt0 value is in the timing diagram shown as a histogram for illustrating the single-slope operation. the diagram includes non-inverted and inverted pwm outputs. the small horizontal line marks on the tcnt0 slopes represent compare matches between ocr0x and tcnt0. f ocnx f clk_i/o 2 n 1 ocrna + () ?? -------------------------------------------------- - =
66 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 11-6. fast pwm mode, timing diagram the timer/counter overflow flag (tov0) is set each time the counter reaches top. if the interrupt is enabled, the interrupt handler routine can be used for updating the compare value. in fast pwm mode, the compare unit allows generation of pwm waveforms on the oc0x pins. setting the com0x[1:0] bits to two will produce a non-inverted pwm and an inverted pwm output can be generated by setting the com0x[1:0] to three: setting the com0a[1:0] bits to one allows the oc0a pin to toggle on compare matches if the wgm02 bit is set. this option is not available for the oc0b pin (see table 11-3 on page 70 ). the actual oc0x value will only be visible on the port pin if the data direction for the port pin is set as output. the pwm waveform is generated by setting (or clearing) the oc0x register at the compare match between ocr0x and tcnt0, and clearing (or setting) the oc0x register at the timer clock cycle the counter is cleared (changes from top to bottom). the pwm frequency for the output can be calculated by the following equation: the n variable represents the prescale factor (1, 8, 64, 256, or 1024). the extreme values for the ocr0x register represents special cases when generating a pwm waveform output in the fast pwm mode. if ocr0x is set equal to bottom, the output will be a narrow spike for each top+1 timer clock cycle. setting the ocr0x equal to top will result in a constantly high or low output (depending on the polarity of the output set by the com0x[1:0] bits.) a frequency (with 50% duty cycle) waveform output in fast pwm mode can be achieved by setting oc0a to toggle its logical level on each compare match (com0a[1:0] = 1). the waveform generated will have a maximum frequency of f clk_i/o /2 when ocr0a is set to zero. this feature is similar to the oc0a toggle in ctc mode, except the double buffer feature of the output compare unit is enabled in the fast pwm mode. 11.7.4 phase correct pwm mode the phase correct pwm mode (wgm0[2:0] = 1 or 5) provides a high resolution phase correct pwm waveform generation option. the phase correct pwm mode is based on a dual-slope operation. the counter counts repeatedly from bottom to top and then from top to bottom. top is defined as 0xff when wgm0[2:0] = 1, and ocr0a when wgm0[2:0] = 5. in non-inverting compare output mode, the output compare (oc0x) is cleared on the compare match between tcnt0 and ocr0x while upcounting, and set on the compare match while down-counting. in inverting output compare t cntn ocrnx update and tovn interrupt flag set 1 p eriod 2 3 o cnx o cnx (comnx[1:0] = 2) (comnx[1:0] = 3) ocrnx interrupt flag se t 4 5 6 7 f ocnxpwm f clk_i/o n top 1 + ( ) ? ---------------------------------- - =
67 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 mode, the operation is inverted. the dual-slope operation has lower maximum operation frequency than single slope operation. however, due to the symmetric feature of the dual-slope pwm modes, these modes are preferred for motor control applications. figure 11-7. phase correct pwm mode, timing diagram in phase correct pwm mode the counter is incremented until the counter value matches top. when the counter reaches top, it changes the count direction. the tcnt0 value will be equal to top for one timer clock cycle. the timing diagram for the phase correct pwm mode is shown on figure 11-7 . the tcnt0 value is in the timing diagram shown as a histogram for illustrating the dual-slope operation. the diagram includes non-inverted and inverted pwm outputs. the small horizontal line marks on the tcnt0 slopes represent compare matches between ocr0x and tcnt0. the timer/counter overflow flag (tov0) is set each time the counter reaches bottom. the interrupt flag can be used to generate an interrupt each time the counter reaches the bottom value. in phase correct pwm mode, the compare unit allows generation of pwm waveforms on the oc0x pins. setting the com0x[1:0] bits to two will produce a non-inverted pwm. an inverted pwm output can be generated by setting the com0x[1:0] to three: setting the com0a0 bits to one allows the oc0a pin to toggle on compare matches if the wgm02 bit is set. this option is not available for the oc0b pin (see table 11-4 on page 70 ). the actual oc0x value will only be visible on the port pin if the data direction for the port pin is set as output. the pwm waveform is generated by clearing (or setting) the oc0x register at the compare match between ocr0x and tcnt0 when the counter increments, and setting (or clearing) the oc0x register at compare match between ocr0x and tcnt0 when the counter decrements. the pwm frequency for the output when using phase correct pwm can be calculated by the following equation: the n variable represents the prescale factor (1, 8, 64, 256, or 1024). the extreme values for the ocr0x register represent special cases when generating a pwm waveform output in the phase correct pwm mode. if the ocr0x is set equal to bottom, the output will be continuously low and if set equal to tovn interr u pt fl a g s et ocnx interr u pt fl a g s et 1 2 3 tcntn period ocnx ocxn (comnx[1:0] = 2) (comnx[1:0] = 3) ocrnx upd a te f ocnxpcpwm f clk_i/o 2 n top ------------------------------- - =
68 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 top the output will be continuously high for non-inverted pwm mode. for inverted pwm the output will have the opposite logic values. at the very start of period 2 in figure 11-7 on page 67 ocnx has a transition from high to low even though there is no compare match. the point of this transition is to guarantee symmetry around bottom. there are two cases that give a transition without compare match. z ocr0x changes its value from top, like in figure 11-7 on page 67 . when the ocr0x value is top the ocnx pin value is the same as the result of a down-counting compare match. to ensure symmetry around bottom the ocnx value at top must correspond to the result of an up-counting compare match. z the timer starts counting from a value higher than the one in ocr0x, and for that reason misses the compare match and hence the ocnx change that would have happened on the way up. 11.8 timer/counter timing diagrams the timer/counter is a synchronous design and the timer clock (clk t0 ) is therefore shown as a clock enable signal in the following figures. the figures include information on when interrupt flags are set. figure 11-8 on page 68 contains timing data for basic timer/counter operation. the figure shows the count sequence close to the max value in all modes other than phase correct pwm mode. figure 11-8. timer/counter timing diagram, no prescaling figure 11-9 on page 68 shows the same timing data, but with the prescaler enabled. figure 11-9. timer/counter timing diagram, with prescaler (f clk_i/o /8) figure 11-10 on page 69 shows the setting of ocf0b in all modes and ocf0a in all modes except ctc mode and pwm mode, where ocr0a is top. clk tn (clk i/o /1) tovn clk i/o tcntn max - 1 max bottom bottom + 1 tovn tcntn max - 1 max bottom bottom + 1 clk i/o clk tn (clk i/o /8)
69 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 11-10.timer/counter timing diagram, setting of ocf0x, with prescaler (f clk_i/o /8) figure 11-11 on page 69 shows the setting of ocf0a and the clearing of tcnt0 in ctc mode and fast pwm mode where ocr0a is top. figure 11-11.timer/counter timing diagram, clear timer on compare match mode, with prescaler (f clk_i/o /8) 11.9 register description 11.9.1 tccr0a ? timer/counter control register a z bits 7:6 ? com0a[1:0]: compare match output a mode these bits control the output compare pin (oc0a) behavior. if one or both of the com0a[1:0] bits are set, the oc0a output overrides the normal port functionality of the i/o pin it is connected to. however, note that the data direction register (ddr) bit corresponding to the oc0a pin must be set in order to enable the output driver. ocfnx ocrnx tcntn ocrnx value ocrnx - 1 ocrnx ocrnx + 1 ocrnx + 2 clk i/o clk tn (clk i/o /8) ocfnx ocrnx tcntn (ctc) top top - 1 top bottom bottom + 1 clk i/o clk tn (clk i/o /8) bit 7 6 5 4 3 2 1 0 0x19 com0a1 com0a0 com0b1 com0b0 ? ? wgm01 wgm00 tccr0a read/write r/w r/w r/w r/w r r r/w r/w initial value 0 0 0 0 0 0 0 0
70 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 when oc0a is connected to the pin, the function of the com0a[1:0] bits depends on the wgm0[2:0] bit setting. table 11-2 shows the com0a[1:0] bit functionality when the wgm0[2:0] bits are set to a normal or ctc mode (non-pwm). table 11-2. compare output mode, non-pwm mode table 11-3 shows com0a[1:0] bit functionality when wgm0[2:0] bits are set to fast pwm mode. table 11-3. compare output mode, fast pwm mode note: a special case occurs when ocr0a equals top and com0a1 is set. in this case, the compare match is ignored, but the set or clear is done at bottom. see ?fast pwm mode? on page 65 for more details. table 11-4 shows com0a[1:0] bit functionality when wgm0[2:0] bits are set to phase correct pwm mode. table 11-4. compare output mode, phase correct pwm mode note: when ocr0a equals top and com0a1 is set, the compare match is ignored, but the set or clear is done at top. see ?phase correct pwm mode? on page 66 for more details. z bits 5:4 ? com0b[1:0]: compare match output b mode com0a1 com0a0 description 0 0 normal port operation, oc0a disconnected. 0 1 toggle oc0a on compare match 1 0 clear oc0a on compare match 1 1 set oc0a on compare match com0a1 com0a0 description 0 0 normal port operation, oc0a disconnected 0 1 wgm02 = 0: normal port operation, oc0a disconnected wgm02 = 1: toggle oc0a on compare match 1 0 clear oc0a on compare match set oc0a at bottom (non-inverting mode) 1 1 set oc0a on compare match clear oc0a at bottom (inverting mode) com0a1 com0a0 description 0 0 normal port operation, oc0a disconnected. 0 1 wgm02 = 0: normal port operation, oc0a disconnected. wgm02 = 1: toggle oc0a on compare match. 1 0 clear oc0a on compare match when up-counting. set oc0a on compare match when down-counting. 1 1 set oc0a on compare match when up-counting. clear oc0a on compare match when down-counting.
71 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 these bits control the output compare pin (oc0b) behavior. if one or both of com0b[1:0] bits are set, the oc0b output overrides the normal port functionality of the i/o pin it is connected to. the data direction register (ddr) bit corresponding to the oc0b pin must be set in order to enable the output driver. when oc0b is connected to the pin, the function of com0b[1:0] bits depend on wgm0[2:0] bit setting. table 11-5 shows com0b[1:0] bit functionality when wgm0[2:0] bits are set to normal or ctc mode (non-pwm). table 11-5. compare output mode, non-pwm mode table 11-6 shows com0b[1:0] bit functionality when wgm0[2:0] bits are set to fast pwm mode. table 11-6. compare output mode, fast pwm mode note: a special case occurs when ocr0b equals top and com0b1 is set. in this case, the compare match is ignored, but the set or clear is done at bottom. see ?fast pwm mode? on page 65 for more details. table 11-7 shows the com0b[1:0] bit functionality when the wgm0[2:0] bits are set to phase correct pwm mode. table 11-7. compare output mode, phase correct pwm mode note: a special case occurs when ocr0b equals top and com0b1 is set. in this case, the compare match is ignored, but the set or clear is done at top. see ?phase correct pwm mode? on page 66 for more details. com0b1 com0b0 description 0 0 normal port operation, oc0b disconnected. 0 1 toggle oc0b on compare match 1 0 clear oc0b on compare match 1 1 set oc0b on compare match com0b1 com0b0 description 0 0 normal port operation, oc0b disconnected. 0 1 reserved 1 0 clear oc0b on compare match, set oc0b at bottom (non-inverting mode) 1 1 set oc0b on compare match, clear oc0b at bottom (inverting mode) com0b1 com0b0 description 0 0 normal port operation, oc0b disconnected. 0 1 reserved 1 0 clear oc0b on compare match when up-counting. set oc0b on compare match when down-counting. 1 1 set oc0b on compare match when up-counting. clear oc0b on compare match when down-counting.
72 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 z bits 3:2 ? res: reserved bits these bits are reserved and will always read zero. z bits 1:0 ? wgm0[1:0]: waveform generation mode combined with the wgm02 bit found in the tccr0b register, these bits control the counting sequence of the counter, the source for maximum (top) counter value, and what type of waveform generation to be used, see table 11-8 . modes of operation supported by the timer/counter unit are: normal mode (counter), clear timer on compare match (ctc) mode, and two types of pulse width modulation (pwm) modes (see ?modes of operation? on page 64 ). table 11-8. waveform generation mode bit description note: 1. max = 0xff bottom = 0x00 11.9.2 tccr0b ? timer/counter control register b z bit 7 ? foc0a: force output compare a the foc0a bit is only active when the wgm bits specify a non-pwm mode. however, for ensuring compatibility with future devices, this bit must be set to zero when tccr0b is written when operating in pwm mode. when writing a logical one to the foc0a bit, an immediate compare match is forced on the waveform generation unit. the oc0a output is changed according to its com0a[1:0] bits setting. note that the foc0a bit is implemented as a strobe. therefore it is the value present in the com0a[1:0] bits that determines the effect of the forced compare. a foc0a strobe will not generate any interrupt, nor will it clear the timer in ctc mode using ocr0a as top. the foc0a bit always reads as zero. z bit 6 ? foc0b: force output compare b the foc0b bit is only active when the wgm bits specify a non-pwm mode. mode wgm02 wgm01 wgm00 timer/counter mode of operation top update of ocrx at tov flag set on (1) 0 0 0 0 normal 0xff immediate max 1 0 0 1 pwm, phase correct 0xff top bottom 2 0 1 0 ctc ocra immediate max 3 0 1 1 fast pwm 0xff bottom max 4 1 0 0 reserved ? ? ? 5 1 0 1 pwm, phase correct ocra top bottom 6 1 1 0 reserved ? ? ? 7 1 1 1 fast pwm ocra bottom top bit 7 6 5 4 3 2 1 0 0x18 foc0a foc0b ? ? wgm02 cs02 cs01 cs00 tccr0b read/write w w r r r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
73 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 however, for ensuring compatibility with future devices, this bit must be set to zero when tccr0b is written when operating in pwm mode. when writing a logical one to the foc0b bit, an immediate compare match is forced on the waveform generation unit. the oc0b output is changed according to its com0b[1:0] bits setting. note that the foc0b bit is implemented as a strobe. therefore it is the value present in the com0b[1:0] bits that determines the effect of the forced compare. a foc0b strobe will not generate any interrupt, nor will it clear the timer in ctc mode using ocr0b as top. the foc0b bit always reads as zero. z bits 5:4 ? res: reserved bits these bits are reserved bits in the attiny20 and will always read as zero. z bit 3 ? wgm02: waveform generation mode see the description in the ?tccr0a ? timer/counter control register a? on page 69 . z bits 2:0 ? cs0[2:0]: clock select the three clock select bits select the clock source to be used by the timer/counter. table 11-9. clock select bit description if external pin modes are used for the timer/counter0, transitions on the t0 pin will clock the counter even if the pin is configured as an output. this feature allows software control of the counting. 11.9.3 tcnt0 ? timer/counter register the timer/counter register gives direct access, both for read and write operations, to the timer/counter unit 8-bit counter. writing to the tcnt0 register blocks (removes) the compare match on the following timer clock. modifying the counter (tcnt0) while the counter is running, introduces a risk of missing a compare match between tcnt0 and the ocr0x registers. cs02 cs01 cs00 description 0 0 0 no clock source (timer/counter stopped) 0 0 1 clk i/o /(no prescaling) 0 1 0 clk i/o /8 (from prescaler) 0 1 1 clk i/o /64 (from prescaler) 1 0 0 clk i/o /256 (from prescaler) 1 0 1 clk i/o /1024 (from prescaler) 1 1 0 external clock source on t0 pin. clock on falling edge. 1 1 1 external clock source on t0 pin. clock on rising edge. bit 76543210 0x17 tcnt0[7:0] tcnt0 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 00000000
74 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 11.9.4 ocr0a ? output compare register a the output compare register a contains an 8-bit value that is continuously compared with the counter value (tcnt0). a match can be used to generate an output compare interrupt, or to generate a waveform output on the oc0a pin. 11.9.5 ocr0b ? output compare register b the output compare register b contains an 8-bit value that is continuously compared with the counter value (tcnt0). a match can be used to generate an output compare interrupt, or to generate a waveform output on the oc0b pin. 11.9.6 timsk ? timer/counter interrupt mask register z bit 6 ? res: reserved bit this bit is reserved and will always read as zero. z bit 2 ? ocie0b: timer/counter output compare match b interrupt enable when the ocie0b bit is written to one, and the i-bit in the status register is set, the timer/counter compare match b interrupt is enabled. the corresponding interrupt is executed if a compare match in timer/counter occurs, i.e., when the ocf0b bit is set in the timer/counter interrupt flag register ? tifr. z bit 1 ? ocie0a: timer/counter0 output compare match a interrupt enable when the ocie0a bit is written to one, and the i-bit in the status register is set, the timer/counter0 compare match a interrupt is enabled. the corresponding interrupt is executed if a compare match in timer/counter0 occurs, i.e., when the ocf0a bit is set in the timer/counter interrupt flag register ? tifr. z bit 0 ? toie0: timer/counter0 overflow interrupt enable when the toie0 bit is written to one, and the i-bit in the status register is set, the timer/counter0 overflow interrupt is enabled. the corresponding interrupt is executed if an overflow in timer/counter0 occurs, i.e., when the tov0 bit is set in the timer/counter interrupt flag register ? tifr. bit 76543210 0x16 ocr0a[7:0] ocr0a read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 00000000 bit 76543210 0x15 ocr0b[7:0] ocr0b read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 00000000 bit 7 6 5 4 3 2 1 0 0x26 icie1 ? ocie1b ocie1a toie1 ocie0b ocie0a toie0 timsk read/write r/w r r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
75 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 11.9.7 tifr ? timer/counter interrupt flag register z bit 6 ? res: reserved bit this bit is reserved and will always read as zero. z bit 2 ? ocf0b: output compare flag 0 b the ocf0b bit is set when a compare match occurs between the timer/counter and the data in ocr0b ? output compare register0 b. ocf0b is cleared by hardware when executing the corresponding interrupt handling vector. alternatively, ocf0b is cleared by writing a logic one to the flag. when the i-bit in sreg, ocie0b (timer/counter compare b match interrupt enable), and ocf0b are set, the timer/counter compare match interrupt is executed. z bit 1 ? ocf0a: output compare flag 0 a the ocf0a bit is set when a compare match occurs between the timer/counter0 and the data in ocr0a ? output compare register0. ocf0a is cleared by hardware when executing the corresponding interrupt handling vector. alternatively, ocf0a is cleared by writing a logic one to the flag. when the i-bit in sreg, ocie0a (timer/counter0 compare match interrupt enable), and ocf0a are set, the timer/counter0 compare match interrupt is executed. z bit 0 ? tov0: timer/counter0 overflow flag the bit tov0 is set when an overflow occurs in timer/counter0. tov0 is cleared by hardware when executing the corresponding interrupt handling vector. alternatively, tov0 is cleared by writing a logic one to the flag. when the sreg i-bit, toie0 (timer/counter0 overflow interrupt enable), and tov0 are set, the timer/counter0 overflow interrupt is executed. the setting of this flag is dependent of the wgm0[2:0] bit setting. see table 11-8 on page 72 and table 12-5 on page 98 . bit 76543210 0x25 icf1 ? ocf1b ocf1a tov1 ocf0b ocf0a tov0 tifr read/write r/w r r/w r/w r/w r/w r/w r/w initial value 00000000
76 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 12. 16-bit timer/counter1 12.1 features z true 16-bit design (i.e., allows 16-bit pwm) z two independent output compare units z double buffered output compare registers z one input capture unit z input capture noise canceler z clear timer on compare match (auto reload) z glitch-free, phase correct pulse width modulator (pwm) z variable pwm period z frequency generator z external event counter z four independent interrupt sources (tov1, ocf1a, ocf1b, and icf1) 12.2 overview the 16-bit timer/counter unit allows accurate program execution timing (event management), wave generation, and signal timing measurement. a simplified block diagram of the 16-bit timer/counter is shown in figure 12-1 on page 76 . for actual placement of i/o pins, refer to ?soic/tssop? on page 2 . cpu accessible i/o registers, including i/o bits and i/o pins, are shown in bold. the device-specific i/o register and bit locations are listed in section ?register description? on page 96 . figure 12-1. 16-bit timer/counter block diagram clock s elect timer/co u nter data b u s ocrna ocrnb icrn = = tcntn w a veform gener a tion w a veform gener a tion ocna ocnb noi s e c a nceler icpn = fixed top v a l u e s edge detector control logic = 0 top bottom co u nt cle a r direction tovn (int.req.) ocna (int.req.) ocnb (int.req.) icfn (int.req.) tccrna tccrnb ( from an a log comp a r a tor o u p u t ) tn edge detector ( from pre s c a ler ) clk tn
77 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 most register and bit references in this section are written in general form. a lower case ?n? replaces the timer/counter number, and a lower case ?x? replaces the output compare unit channel. however, when using the register or bit defines in a program, the precise form must be used, i.e., tcnt1 for accessing timer/counter1 counter value and so on. 12.2.1 registers the timer/counter (tcnt1), output compare registers (ocr1a/b), and input capture register (icr1) are all 16-bit registers. special procedures must be followed when accessing the 16-bit registers. these procedures are described in section ?accessing 16-bit registers? on page 93 . the timer/counter control registers (tccr1a/b) are 8-bit registers and have no cpu access restrictions. interrupt requests (abbreviated to int.req. in the figure) signals are all visible in the timer interrupt flag register (tifr). all interrupts are individually masked with the timer interrupt mask register (timsk). tifr and timsk are not shown in the figure. the timer/counter can be clocked internally, via the prescaler, or by an external clock source on the t1 pin. the clock select logic block controls which clock source and edge the timer/counter uses to increment (or decrement) its value. the timer/counter is inactive when no clock source is selected. the output from the clock select logic is referred to as the timer clock (clk t1 ). the double buffered output compare registers (ocr1a/b) are compared with the timer/counter value at all time. the result of the compare can be used by the waveform generator to generate a pwm or variable frequency output on the output compare pin (oc1a/b). see ?output compare units? on page 80 . the compare match event will also set the compare match flag (ocf1a/b) which can be used to generate an output compare interrupt request. the input capture register can capture the timer/counter value at a given external (edge triggered) event on either the input capture pin (icp1) or on the analog comparator pins (see ?analog comparator? on page 105 ). the input capture unit includes a digital filtering unit (noise canceler) for reducing the chance of capturing noise spikes. the top value, or maximum timer/counter value, can in some modes of operation be defined by either the ocr1a register, the icr1 register, or by a set of fixed values. when using ocr1a as top value in a pwm mode, the ocr1a register can not be used for generating a pwm output. however, the top value will in this case be double buffered allowing the top value to be changed in run time. if a fixed top value is required, the icr1 register can be used as an alternative, freeing the ocr1a to be used as pwm output. 12.2.2 definitions the following definitions are used extensively throughout the section: table 12-1. definitions 12.3 timer/counter clock sources the timer/counter can be clocked by an internal or an external clock source. the clock source is selected by the clock select logic which is controlled by the clock select (cs1[2:0]) bits located in the timer/counter control register b (tccr1b). for details on clock sources and prescaler, see ?timer/counter prescaler? on page 103 . constant description bottom the counter reaches bottom when it becomes 0x00 max the counter reaches its maximum when it becomes 0xff (decimal 255) top the counter reaches top when it becomes equal to the highest value in the count sequence. the top value can be assigned to be the fixed value 0xff (max), the value stored in the ocr 1 a register, or the value stored in the icr1 register. the assignment depends on the mode of operation
78 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 12.4 counter unit the main part of the 16-bit timer/counter is the programmable 16-bit bi-directional counter unit. figure 12-2 shows a block diagram of the counter and its surroundings. figure 12-2. counter unit block diagram description of internal signals used in figure 12-2 : count increment or decrement tcnt1 by 1. direction select between increment and decrement. clear clear tcnt1 (set all bits to zero). clk t1 timer/counter1 clock. top signalize that tcnt1 has reached maximum value. bottom signalize that tcnt1 has reached minimum value (zero). the 16-bit counter is mapped into two 8-bit i/o memory locations: counter high (tcnt1h) containing the upper eight bits of the counter, and counter low (tcnt1l) containing the lower eight bits. the tcnt1h register can only be indirectly accessed by the cpu. when the cpu does an access to the tcnt1h i/o location, the cpu accesses the high byte temporary register (temp). the temporary register is updated with the tcnt1h value when the tcnt1l is read, and tcnt1h is updated with the temporary register value when tcnt1l is written. this allows the cpu to read or write the entire 16-bit counter value within one clock cycle via the 8-bit data bus. it is important to notice that there are special cases of writing to the tcnt1 register when the counter is counting that will give unpredictable results. the special cases are described in the sections where they are of importance. depending on the mode of operation used, the counter is cleared, incremented, or decremented at each timer clock (clk t1 ). the clk t1 can be generated from an external or internal clock source, selected by the clock select bits (cs1[2:0]). when no clock source is selected (cs1[2:0] = 0) the timer is stopped. however, the tcnt1 value can be accessed by the cpu, independent of whether clk t1 is present or not. a cpu write overrides (has priority over) all counter clear or count operations. the counting sequence is determined by the setting of the waveform generation mode bits (wgm1[3:0]) located in the timer/counter control registers a and b (tccr1a and tccr1b). there are close connections between how the counter behaves (counts) and how waveforms are generated on the output compare outputs oc1x. for more details about advanced counting sequences and waveform generation, see ?modes of operation? on page 83 . the timer/counter overflow flag (tov1) is set according to the mode of operation selected by the wgm1[3:0] bits. tov1 can be used for generating a cpu interrupt. 12.5 input capture unit the timer/counter incorporates an input capture unit that can capture external events and give them a time-stamp indicating time of occurrence. the external signal indicating an event, or multiple events, can be applied via the icp1 pin temp (8-bit) data bus (8-bit) tcntn (16-bit counter) tcntnh (8-bit) tcntnl (8-bit) control logic count clear direction tovn (int.re q .) clock select top bottom tn edge detector ( from prescaler ) clk tn
79 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 or alternatively, via the analog-comparator unit. the time-stamps can then be used to calculate frequency, duty-cycle, and other features of the signal applied. alternatively the time-stamps can be used for creating a log of the events. the input capture unit is illustrated by the block diagram shown in figure 12-3 . the elements of the block diagram that are not directly a part of the input capture unit are gray shaded. the small ?n? in register and bit names indicates the timer/counter number. figure 12-3. input capture unit block diagram when a change of the logic level (an event) occurs on the input capture pin (icp1), alternatively on the analog comparator output (aco), and this change confirms to the setting of the edge detector, a capture will be triggered. when a capture is triggered, the 16-bit value of the counter (tcnt1) is written to the input capture register (icr1). the input capture flag (icf1) is set at the same system clock as the tcnt1 value is copied into icr1 register. if enabled (icie1 = 1), the input capture flag generates an input capture interrupt. the icf1 flag is automatically cleared when the interrupt is executed. alternatively the icf1 flag can be cleared by software by writing a logical one to its i/o bit location. reading the 16-bit value in the input capture register (icr1) is done by first reading the low byte (icr1l) and then the high byte (icr1h). when the low byte is read the high byte is copied into the high byte temporary register (temp). when the cpu reads the icr1h i/o location it will access the temp register. the icr1 register can only be written when using a waveform generation mode that utilizes the icr1 register for defining the counter?s top value. in these cases the waveform generation mode (wgm1[3:0]) bits must be set before the top value can be written to the icr1 register. when writing the icr1 register the high byte must be written to the icr1h i/o location before the low byte is written to icr1l. for more information on how to access the 16-bit registers refer to ?accessing 16-bit registers? on page 93 . 12.5.1 input capture trigger source the main trigger source for the input capture unit is the input capture pin (icp1). timer/counter1 can alternatively use the analog comparator output as trigger source for the input capture unit. the analog comparator is selected as trigger source by setting the analog comparator input capture (acic) bit in the analog comparator control and status register icfn (int.req.) an a log comp a r a tor write icrn (16- b it regi s ter) icrnh ( 8 - b it) noi s e c a nceler icpn edge detector temp ( 8 - b it) data b u s ( 8 - b it) icrnl ( 8 - b it) tcntn (16- b it co u nter) tcntnh ( 8 - b it) tcntnl ( 8 - b it) acic* icncn ices1 aco*
80 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 (acsr). be aware that changing trigger source can trigger a capture. the input capture flag must therefore be cleared after the change. both the input capture pin (icp1) and the analog comparator output (aco) inputs are sampled using the same technique as for the t1 pin ( figure 13-1 on page 103 ). the edge detector is also identical. however, when the noise canceler is enabled, additional logic is inserted before the edge detector, which increases the delay by four system clock cycles. note that the input of the noise canceler and edge detector is always enabled unless the timer/counter is set in a waveform generation mode that uses icr1 to define top. an input capture can be triggered by software by controlling the port of the icp1 pin. 12.5.2 noise canceler the noise canceler uses a simple digital filtering technique to improve noise immunity. consecutive samples are monitored in a pipeline four units deep. the signal going to the edge detector is allowed to change only when all four samples are equal. the noise canceler is enabled by setting the input capture noise canceler (icnc1) bit in timer/counter control register b (tccr1b). when enabled, the noise canceler introduces an additional delay of four system clock cycles to a change applied to the input and before icr1 is updated. the noise canceler uses the system clock directly and is therefore not affected by the prescaler. 12.5.3 using the input capture unit the main challenge when using the input capture unit is to assign enough processor capacity for handling the incoming events. the time between two events is critical. if the processor has not read the captured value in the icr1 register before the next event occurs, the icr1 will be overwritten with a new value. in this case the result of the capture will be incorrect. when using the input capture interrupt, the icr1 register should be read as early in the interrupt handler routine as possible. even though the input capture interrupt has relatively high priority, the maximum interrupt response time is dependent on the maximum number of clock cycles it takes to handle any of the other interrupt requests. using the input capture unit in any mode of operation when the top value (resolution) is actively changed during operation, is not recommended. measurement of an external signal?s duty cycle requires that the trigger edge is changed after each capture. changing the edge sensing must be done as early as possible after the icr1 register has been read. after a change of the edge, the input capture flag (icf1) must be cleared by software (writing a logical one to the i/o bit location). for measuring frequency only, the clearing of the icf1 flag is not required (if an interrupt handler is used). 12.6 output compare units the 16-bit comparator continuously compares tcnt1 with the output compare register (ocr1x). if tcnt equals ocr1x the comparator signals a match. a match will set the output compare flag (ocf1x) at the next timer clock cycle. if enabled (ocie1x = 1), the output compare flag generates an output compare interrupt. the ocf1x flag is automatically cleared when the interrupt is executed. alternatively the ocf1x flag can be cleared by software by writing a logical one to its i/o bit location. the waveform generator uses the match signal to generate an output according to operating mode set by the waveform generation mode (wgm1[3:0]) bits and compare output mode (com1x[1:0]) bits. the top and bottom signals are used by the waveform generator for handling the special cases of the extreme values in some modes of operation ( ?modes of operation? on page 83 ). a special feature of output compare unit a allows it to define the timer/counter top value (i.e., counter resolution). in addition to the counter resolution, the top value defines the period time for waveforms generated by the waveform generator.
81 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 12-4. output compare unit, block diagram figure 12-4 on page 81 shows a block diagram of the output compare unit. the small ?n? in the register and bit names indicates the device number (n = 1 for timer/counter 1), and the ?x? indicates output compare unit (a/b). the elements of the block diagram that are not directly a part of the output compare unit are gray shaded. the ocr1x register is double buffered when using any of the twelve pulse width modulation (pwm) modes. for the normal and clear timer on compare (ctc) modes of operation, the double buffering is disabled. the double buffering synchronizes the update of the ocr1x compare register to either top or bottom of the counting sequence. the synchronization prevents the occurrence of odd-length, non-symmetrical pwm pulses, thereby making the output glitch- free. the ocr1x register access may seem complex, but this is not case. when the double buffering is enabled, the cpu has access to the ocr1x buffer register, and if double buffering is disabled the cpu will access the ocr1x directly. the content of the ocr1x (buffer or compare) register is only changed by a write operation (the timer/counter does not update this register automatically as the tcnt1 and icr1 register). therefore ocr1x is not read via the high byte temporary register (temp). however, it is a good practice to read the low byte first as when accessing other 16-bit registers. writing the ocr1x registers must be done via the temp register since the compare of all 16 bits is done continuously. the high byte (ocr1xh) has to be written first. when the high byte i/o location is written by the cpu, the temp register will be updated by the value written. then when the low byte (ocr1xl) is written to the lower eight bits, the high byte will be copied into the upper 8-bits of either the ocr1x buffer or ocr1x compare register in the same system clock cycle. for more information of how to access the 16-bit registers refer to ?accessing 16-bit registers? on page 93 . 12.6.1 force output compare in non-pwm waveform generation modes, the match output of the comparator can be forced by writing a one to the force output compare (1x) bit. forcing compare match will not set the ocf1x flag or reload/clear the timer, but the ocfnx (int.req.) = (16-bit comparator ) ocrnx buffer (16-bit register) ocrnxh buf. (8-bit) ocnx temp (8-bit) data bus (8-bit) ocrnxl buf. (8-bit) tcntn (16-bit counter) tcntnh (8-bit) tcntnl (8-bit) comnx[1:0] wgmn[3:0] ocrnx (16-bit register) ocrnxh (8-bit) ocrnxl (8-bit) waveform generator top bottom
82 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 oc1x pin will be updated as if a real compare match had occurred (the com1[1:0] bits settings define whether the oc1x pin is set, cleared or toggled). 12.6.2 compare match blocking by tcnt1 write all cpu writes to the tcnt1 register will block any compare match that occurs in the next timer clock cycle, even when the timer is stopped. this feature allows ocr1x to be initialized to the same value as tcnt1 without triggering an interrupt when the timer/counter clock is enabled. 12.6.3 using the output compare unit since writing tcnt1 in any mode of operation will block all compare matches for one timer clock cycle, there are risks involved when changing tcnt1 when using any of the output compare channels, independent of whether the timer/counter is running or not. if the value written to tcnt1 equals the ocr1x value, the compare match will be missed, resulting in incorrect waveform generation. do not write the tcnt1 equal to top in pwm modes with variable top values. the compare match for the top will be ignored and the counter will continue to 0xffff. similarly, do not write the tcnt1 value equal to bottom when the counter is downcounting. the setup of the oc1x should be performed before setting the data direction register for the port pin to output. the easiest way of setting the oc1x value is to use the force output compare (1x) strobe bits in normal mode. the oc1x register keeps its value even when changing between waveform generation modes. be aware that the com1x[1:0] bits are not double buffered together with the compare value. changing the com1x[1:0] bits will take effect immediately. 12.7 compare match output unit the compare output mode (com1x[1:0]) bits have two functions. the waveform generator uses the com1x[1:0] bits for defining the output compare (oc1x) state at the next compare match. secondly the com1x[1:0] bits control the oc1x pin output source. figure 12-5 shows a simplified schematic of the logic affected by the com1x[1:0] bit setting. the i/o registers, i/o bits, and i/o pins in the figure are shown in bold. only the parts of the general i/o port control registers (ddr and port) that are affected by the com1x[1:0] bits are shown. when referring to the oc1x state, the reference is for the internal oc1x register, not the oc1x pin. if a system reset occur, the oc1x register is reset to ?0?.
83 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 12-5. compare match output unit, schematic (non-pwm mode) the general i/o port function is overridden by the output compare (oc1x) from the waveform generator if either of the com1x[1:0] bits are set. however, the oc1x pin direction (input or output) is still controlled by the data direction register (ddr) for the port pin. the data direction register bit for the oc1x pin (ddr_oc1x) must be set as output before the oc1x value is visible on the pin. the port override function is generally independent of the waveform generation mode, but there are some exceptions. see table 12-2 on page 97 , table 12-3 on page 97 and table 12-4 on page 97 for details. the design of the output compare pin logic allows initialization of the oc1x state before the output is enabled. note that some com1x[1:0] bit settings are reserved for certain modes of operation. see ?register description? on page 96 the com1x[1:0] bits have no effect on the input capture unit. 12.7.1 compare output mode and waveform generation the waveform generator uses the com1x[1:0] bits differently in normal, ctc, and pwm modes. for all modes, setting the com1x[1:0] = 0 tells the waveform generator that no action on the oc1x register is to be performed on the next compare match. for compare output actions in the non-pwm modes refer to table 12-2 on page 97 . for fast pwm mode refer to table 12-3 on page 97 , and for phase correct and phase and frequency correct pwm refer to table 12-4 on page 97 . a change of the com1x[1:0] bits state will have effect at the first compare match after the bits are written. for non-pwm modes, the action can be forced to have immediate effect by using the foc1x strobe bits. 12.8 modes of operation the mode of operation, i.e., the behavior of the timer/counter and the output compare pins, is defined by the combination of the waveform generation mode (wgm1[3:0]) and compare output mode (com1x[1:0]) bits. the compare output mode bits do not affect the counting sequence, while the waveform generation mode bits do. the com1x[1:0] bits control whether the pwm output generated should be inverted or not (inverted or non-inverted pwm). port ddr dq dq ocnx pin ocnx dq waveform generator comnx1 comnx0 0 1 data b u s focnx clk i/o
84 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 for non-pwm modes the com1x[1:0] bits control whether the output should be set, cleared or toggle at a compare match ( ?compare match output unit? on page 82 ) for detailed timing information refer to ?timer/counter timing diagrams? on page 91 . 12.8.1 normal mode the simplest mode of operation is the normal mode (wgm1[3:0] = 0). in this mode the counting direction is always up (incrementing), and no counter clear is performed. the counter simply overruns when it passes its maximum 16-bit value (max = 0xffff) and then restarts from the bottom (0x0000). in normal operation the timer/counter overflow flag (tov1) will be set in the same timer clock cycle as the tcnt1 becomes zero. the tov1 flag in this case behaves like a 17th bit, except that it is only set, not cleared. however, combined with the timer overflow interrupt that automatically clears the tov1 flag, the timer resolution can be increased by software. there are no special cases to consider in the normal mode, a new counter value can be written anytime. the input capture unit is easy to use in normal mode. however, observe that the maximum interval between the external events must not exceed the resolution of the counter. if the interval between events are too long, the timer overflow interrupt or the prescaler must be used to extend the resolution for the capture unit. the output compare units can be used to generate interrupts at some given time. using the output compare to generate waveforms in normal mode is not recommended, since this will occupy too much of the cpu time. 12.8.2 clear timer on compare match (ctc) mode in clear timer on compare or ctc mode (wgm1[3:0] = 4 or 12), the ocr1a or icr1 register are used to manipulate the counter resolution. in ctc mode the counter is cleared to zero when the counter value (tcnt1) matches either the ocr1a (wgm1[3:0] = 4) or the icr1 (wgm1[3:0] = 12). the ocr1a or icr1 define the top value for the counter, hence also its resolution. this mode allows greater control of the compare match output frequency. it also simplifies the operation of counting external events. the timing diagram for the ctc mode is shown in figure 12-6 on page 84 . the counter value (tcnt1) increases until a compare match occurs with either ocr1a or icr1, and then counter (tcnt1) is cleared. figure 12-6. ctc mode, timing diagram an interrupt can be generated at each time the counter value reaches the top value by either using the ocf1a or icf1 flag according to the register used to define the top value. if the interrupt is enabled, the interrupt handler routine can be used for updating the top value. however, changing the top to a value close to bottom when the counter is running with none or a low prescaler value must be done with care since the ctc mode does not have the double buffering feature. if the new value written to ocr1a or icr1 is lower than the current value of tcnt1, the counter will miss the compare match. the counter will then have to count to its maximum value (0xffff) and wrap around starting at 0x0000 tcntn ocna (toggle) ocna interrupt flag set or icfn interrupt flag set (interrupt on top) 1 4 period 2 3 (comna[1:0] = 1)
85 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 before the compare match can occur. in many cases this feature is not desirable. an alternative will then be to use the fast pwm mode using ocr1a for defining top (wgm1[3:0] = 15) since the ocr1a then will be double buffered. for generating a waveform output in ctc mode, the oc1a output can be set to toggle its logical level on each compare match by setting the compare output mode bits to toggle mode (com1a[1:0] = 1). the oc1a value will not be visible on the port pin unless the data direction for the pin is set to output (ddr_oc1a = 1). the waveform generated will have a maximum frequency of f clk_i/o /2 when ocr1a is set to zero (0x0000). the waveform frequency is defined by the following equation: the n variable represents the prescaler factor (1, 8, 64, 256, or 1024). as for the normal mode of operation, the tov1 flag is set in the same timer clock cycle that the counter counts from max to 0x0000. 12.8.3 fast pwm mode the fast pulse width modulation or fast pwm mode (wgm1[3:0] = 5, 6, 7, 14, or 15) provides a high frequency pwm waveform generation option. the fast pwm differs from the other pwm options by its single-slope operation. the counter counts from bottom to top then restarts from bottom. in non-inverting compare output mode, the output compare (oc1x) is cleared on the compare match between tcnt1 and ocr1x, and set at bottom. in inverting compare output mode output is set on compare match and cleared at bottom. due to the single-slope operation, the operating frequency of the fast pwm mode can be twice as high as the phase correct and phase and frequency correct pwm modes that use dual-slope operation. this high frequency makes the fast pwm mode well suited for power regulation, rectification, and dac applications. high frequency allows physically small sized external components (coils, capacitors), hence reduces total system cost. the pwm resolution for fast pwm can be fixed to 8-, 9-, or 10-bit, or defined by either icr1 or ocr1a. the minimum resolution allowed is 2-bit (icr1 or ocr1a set to 0x0003), and the maximum resolution is 16-bit (icr1 or ocr1a set to max). the pwm resolution in bits can be calculated by using the following equation: in fast pwm mode the counter is incremented until the counter value matches either one of the fixed values 0x00ff, 0x01ff, or 0x03ff (wgm1[3:0] = 5, 6, or 7), the value in icr1 (wgm1[3:0] = 14), or the value in ocr1a (wgm1[3:0] = 15). the counter is then cleared at the following timer clock cycle. the timing diagram for the fast pwm mode is shown in figure 12-7 on page 86 . the figure shows fast pwm mode when ocr1a or icr1 is used to define top. the tcnt1 value is in the timing diagram shown as a histogram for illustrating the single-slope operation. the diagram includes non- inverted and inverted pwm outputs. the small horizontal line marks on the tcnt1 slopes represent compare matches between ocr1x and tcnt1. the oc1x interrupt flag will be set when a compare match occurs. f ocna f clk_i/o 2 n 1 ocrna + () ?? ------------------------------------------------- - = r fpwm top 1 + () log 2 () log ---------------------------------- =
86 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 12-7. fast pwm mode, timing diagram the timer/counter overflow flag (tov1) is set each time the counter reaches top. in addition the oc1a or icf1 flag is set at the same timer clock cycle as tov1 is set when either ocr1a or icr1 is used for defining the top value. if one of the interrupts are enabled, the interrupt handler routine can be used for updating the top and compare values. when changing the top value the program must ensure that the new top value is higher or equal to the value of all of the compare registers. if the top value is lower than any of the compare registers, a compare match will never occur between the tcnt1 and the ocr1x. note that when using fixed top values the unused bits are masked to zero when any of the ocr1x registers are written. the procedure for updating icr1 differs from updating ocr1a when used for defining the top value. the icr1 register is not double buffered. this means that if icr1 is changed to a low value when the counter is running with none or a low prescaler value, there is a risk that the new icr1 value written is lower than the current value of tcnt1. the result will then be that the counter will miss the compare match at the top value. the counter will then have to count to the max value (0xffff) and wrap around starting at 0x0000 before the compare match can occur. the ocr1a register however, is double buffered. this feature allows the ocr1a i/o location to be written anytime. when the ocr1a i/o location is written the value written will be put into the ocr1a buffer register. the ocr1a compare register will then be updated with the value in the buffer register at the next timer clock cycle the tcnt1 matches top. the update is done at the same timer clock cycle as the tcnt1 is cleared and the tov1 flag is set. using the icr1 register for defining top works well when using fixed top values. by using icr1, the ocr1a register is free to be used for generating a pwm output on oc1a. however, if the base pwm frequency is actively changed (by changing the top value), using the ocr1a as top is clearly a better choice due to its double buffer feature. in fast pwm mode, the compare units allow generation of pwm waveforms on the oc1x pins. setting the com1x[1:0] bits to two will produce a non-inverted pwm and an inverted pwm output can be generated by setting the com1x[1:0] to three (see table 12-3 on page 97 ). the actual oc1x value will only be visible on the port pin if the data direction for the port pin is set as output (ddr_oc1x). the pwm waveform is generated by setting (or clearing) the oc1x register at the compare match between ocr1x and tcnt1, and clearing (or setting) the oc1x register at the timer clock cycle the counter is cleared (changes from top to bottom). the pwm frequency for the output can be calculated by the following equation: the n variable represents the prescaler divider (1, 8, 64, 256, or 1024). t cntn ocrnx/top update and tovn interrupt flag set an d ocna interrupt flag set or icfn interrupt flag set (interrupt on top) 1 7 p eriod 2 3 4 5 6 8 o cnx o cnx (comnx[1:0] = 2) (comnx[1:0] = 3) f ocnxpwm f clk_i/o n 1 top + ( ) ? ---------------------------------- - =
87 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 the extreme values for the ocr1x register represents special cases when generating a pwm waveform output in the fast pwm mode. if the ocr1x is set equal to bottom (0x0000) the output will be a narrow spike for each top+1 timer clock cycle. setting the ocr1x equal to top will result in a constant high or low output (depending on the polarity of the output set by the com1x[1:0] bits.) a frequency (with 50% duty cycle) waveform output in fast pwm mode can be achieved by setting oc1a to toggle its logical level on each compare match (com1a[1:0] = 1). the waveform generated will have a maximum frequency of f clk_i/o /2 when ocr1a is set to zero (0x0000). this feature is similar to the oc1a toggle in ctc mode, except the double buffer feature of the output compare unit is enabled in the fast pwm mode. 12.8.4 phase correct pwm mode the phase correct pulse width modulation or phase correct pwm mode (wgm1[3:0] = 1, 2, 3, 10, or 11) provides a high resolution phase correct pwm waveform generation option. the phase correct pwm mode is, like the phase and frequency correct pwm mode, based on a dual-slope operation. the counter counts repeatedly from bottom (0x0000) to top and then from top to bottom. in non-inverting compare output mode, the output compare (oc1x) is cleared on the compare match between tcnt1 and ocr1x while upcounting, and set on the compare match while downcounting. in inverting output compare mode, the operation is inverted. the dual-slope operation has lower maximum operation frequency than single slope operation. however, due to the symmetric feature of the dual-slope pwm modes, these modes are preferred for motor control applications. the pwm resolution for the phase correct pwm mode can be fixed to 8-, 9-, or 10-bit, or defined by either icr1 or ocr1a. the minimum resolution allowed is 2-bit (icr1 or ocr1a set to 0x0003), and the maximum resolution is 16-bit (icr1 or ocr1a set to max). the pwm resolution in bits can be calculated by using the following equation: in phase correct pwm mode the counter is incremented until the counter value matches either one of the fixed values 0x00ff, 0x01ff, or 0x03ff (wgm1[3:0] = 1, 2, or 3), the value in icr1 (wgm1[3:0] = 10), or the value in ocr1a (wgm1[3:0] = 11). the counter has then reached the top and changes the count direction. the tcnt1 value will be equal to top for one timer clock cycle. the timing diagram for the phase correct pwm mode is shown on figure 12-8 . the figure shows phase correct pwm mode when ocr1a or icr1 is used to define top. the tcnt1 value is in the timing diagram shown as a histogram for illustrating the dual-slope operation. the diagram includes non-inverted and inverted pwm outputs. the small horizontal line marks on the tcnt1 slopes represent compare matches between ocr1x and tcnt1. the oc1x interrupt flag will be set when a compare match occurs. r pcpwm top 1 + () log 2 () log ---------------------------------- =
88 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 12-8. phase correct pwm mode, timing diagram the timer/counter overflow flag (tov1) is set each time the counter reaches bottom. when either ocr1a or icr1 is used for defining the top value, the oc1a or icf1 flag is set accordingly at the same timer clock cycle as the ocr1x registers are updated with the double buffer value (at top). the interrupt flags can be used to generate an interrupt each time the counter reaches the top or bottom value. when changing the top value the program must ensure that the new top value is higher or equal to the value of all of the compare registers. if the top value is lower than any of the compare registers, a compare match will never occur between the tcnt1 and the ocr1x. note that when using fixed top values, the unused bits are masked to zero when any of the ocr1x registers are written. as the third period shown in figure 12-8 illustrates, changing the top actively while the timer/counter is running in the phase correct mode can result in an unsymmetrical output. the reason for this can be found in the time of update of the ocr1x register. since the ocr1x update occurs at top, the pwm period starts and ends at top. this implies that the length of the falling slope is determined by the previous top value, while the length of the rising slope is determined by the new top value. when these two values differ the two slopes of the period will differ in length. the difference in length gives the unsymmetrical result on the output. it is recommended to use the phase and frequency correct mode instead of the phase correct mode when changing the top value while the timer/counter is running. when using a static top value there are practically no differences between the two modes of operation. in phase correct pwm mode, the compare units allow generation of pwm waveforms on the oc1x pins. setting the com1x[1:0] bits to two will produce a non-inverted pwm and an inverted pwm output can be generated by setting the com1x[1:0] to three (see table 12-4 on page 97 ). the actual oc1x value will only be visible on the port pin if the data direction for the port pin is set as output (ddr_oc1x). the pwm waveform is generated by setting (or clearing) the oc1x register at the compare match between ocr1x and tcnt1 when the counter increments, and clearing (or setting) the oc1x register at compare match between ocr1x and tcnt1 when the counter decrements. the pwm frequency for the output when using phase correct pwm can be calculated by the following equation: the n variable represents the prescaler divider (1, 8, 64, 256, or 1024). ocrnx/top update and ocna interrupt flag set or icfn interrupt flag set (interrupt on top) 1 2 3 4 tovn interrupt flag set (interrupt on bottom) t cntn p eriod o cnx o cnx (comnx[1:0] = 2) (comnx[1:0] = 3) f ocnxpcpwm f clk_i/o 2 n top ?? ---------------------------- =
89 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 the extreme values for the ocr1x register represent special cases when generating a pwm waveform output in the phase correct pwm mode. if the ocr1x is set equal to bottom the output will be continuously low and if set equal to top the output will be continuously high for non-inverted pwm mode. for inverted pwm the output will have the opposite logic values. 12.8.5 phase and frequency correct pwm mode the phase and frequency correct pulse width modulation, or phase and frequency correct pwm mode (wgm1[3:0] = 8 or 9) provides a high resolution phase and frequency correct pwm waveform generation option. the phase and frequency correct pwm mode is, like the phase correct pwm mode, based on a dual-slope operation. the counter counts repeatedly from bottom (0x0000) to top and then from top to bottom. in non-inverting compare output mode, the output compare (oc1x) is cleared on the compare match between tcnt1 and ocr1x while upcounting, and set on the compare match while downcounting. in inverting compare output mode, the operation is inverted. the dual- slope operation gives a lower maximum operation frequency compared to the single-slope operation. however, due to the symmetric feature of the dual-slope pwm modes, these modes are preferred for motor control applications. the main difference between the phase correct, and the phase and frequency correct pwm mode is the time the ocr1x register is updated by the ocr1x buffer register, (see figure 12-8 on page 88 and figure 12-9 on page 90 ). the pwm resolution for the phase and frequency correct pwm mode can be defined by either icr1 or ocr1a. the minimum resolution allowed is 2-bit (icr1 or ocr1a set to 0x0003), and the maximum resolution is 16-bit (icr1 or ocr1a set to max). the pwm resolution in bits can be calculated using the following equation: in phase and frequency correct pwm mode the counter is incremented until the counter value matches either the value in icr1 (wgm1[3:0] = 8), or the value in ocr1a (wgm1[3:0] = 9). the counter has then reached the top and changes the count direction. the tcnt1 value will be equal to top for one timer clock cycle. the timing diagram for the phase correct and frequency correct pwm mode is shown on figure 12-9 . the figure shows phase and frequency correct pwm mode when ocr1a or icr1 is used to define top. the tcnt1 value is in the timing diagram shown as a histogram for illustrating the dual-slope operation. the diagram includes non-inverted and inverted pwm outputs. the small horizontal line marks on the tcnt1 slopes represent compare matches between ocr1x and tcnt1. the oc1x interrupt flag will be set when a compare match occurs. r pfcpwm top 1 + () log 2 () log ---------------------------------- =
90 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 12-9. phase and frequency correct pwm mode, timing diagram the timer/counter overflow flag (tov1) is set at the same timer clock cycle as the ocr1x registers are updated with the double buffer value (at bottom). when either ocr1a or icr1 is used for defining the top value, the oc1a or icf1 flag set when tcnt1 has reached top. the interrupt flags can then be used to generate an interrupt each time the counter reaches the top or bottom value. when changing the top value the program must ensure that the new top value is higher or equal to the value of all of the compare registers. if the top value is lower than any of the compare registers, a compare match will never occur between the tcnt1 and the ocr1x. as figure 12-9 shows the output generated is, in contrast to the phase correct mode, symmetrical in all periods. since the ocr1x registers are updated at bottom, the length of the rising and the falling slopes will always be equal. this gives symmetrical output pulses and is therefore frequency correct. using the icr1 register for defining top works well when using fixed top values. by using icr1, the ocr1a register is free to be used for generating a pwm output on oc1a. however, if the base pwm frequency is actively changed by changing the top value, using the ocr1a as top is clearly a better choice due to its double buffer feature. in phase and frequency correct pwm mode, the compare units allow generation of pwm waveforms on the oc1x pins. setting the com1x[1:0] bits to two will produce a non-inverted pwm and an inverted pwm output can be generated by setting the com1x[1:0] to three (see table 12-4 on page 97 ). the actual oc1x value will only be visible on the port pin if the data direction for the port pin is set as output (ddr_oc1x). the pwm waveform is generated by setting (or clearing) the oc1x register at the compare match between ocr1x and tcnt1 when the counter increments, and clearing (or setting) the oc1x register at compare match between ocr1x and tcnt1 when the counter decrements. the pwm frequency for the output when using phase and frequency correct pwm can be calculated by the following equation: the n variable represents the prescaler divider (1, 8, 64, 256, or 1024). the extreme values for the ocr1x register represents special cases when generating a pwm waveform output in the phase correct pwm mode. if the ocr1x is set equal to bottom the output will be continuously low and if set equal to ocrnx/top updateand tovn interrupt flag set (interrupt on bottom) ocna interrupt flag set or icfn interrupt flag se t (interrupt on top) 1 2 3 4 t cntn p eriod o cnx o cnx (comnx[1:0] = 2) (comnx[1:0] = 3) f ocnxpfcpwm f clk_i/o 2 n top ?? ---------------------------- =
91 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 top the output will be set to high for non-inverted pwm mode. for inverted pwm the output will have the opposite logic values. 12.9 timer/counter timing diagrams the timer/counter is a synchronous design and the timer clock (clk t1 ) is therefore shown as a clock enable signal in the following figures. the figures include information on when interrupt flags are set, and when the ocr1x register is updated with the ocr1x buffer value (only for modes utilizing double buffering). figure 12-10 shows a timing diagram for the setting of ocf1x. figure 12-10.timer/counter timing diagram, setting of ocf1x, no prescaling figure 12-11 shows the same timing data, but with the prescaler enabled. tovn (fpwm) and icfn (if used as top) ocrnx (update at top) tcntn (ctc and fpwm) tcntn (pc and pfc pwm) top - 1 top top - 1 top - 2 old ocrnx value new ocrnx value top - 1 top bottom bottom + 1 clk tn (clk i/o /1) clk i/o
92 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 12-11.timer/counter timing diagram, setting of ocf1x, with prescaler (f clk_i/o /8) figure 12-12 shows the count sequence close to top in various modes. when using phase and frequency correct pwm mode the ocr1x register is updated at bottom. the timing diagrams will be the same, but top should be replaced by bottom, top-1 by bottom+1 and so on. the same renaming applies for modes that set the tov1 flag at bottom. figure 12-12.timer/counter timing diagram, no prescaling figure 12-13 shows the same timing data, but with the prescaler enabled. ocfnx ocrnx tcntn ocrnx value ocrnx - 1 ocrnx ocrnx + 1 ocrnx + 2 clk i/o clk tn (clk i/o /8) clk tn (clk i/o /1) ocfnx clk i/o ocrnx tcntn ocrnx value ocrnx - 1 ocrnx ocrnx + 1 ocrnx + 2
93 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 12-13.timer/counter timing diagram, with prescaler (f clk_i/o /8) 12.10 accessing 16-bit registers the tcnt1, ocr1a/b, and icr1 are 16-bit registers that can be accessed by the avr cpu via the 8-bit data bus. the 16-bit register must be byte accessed using two read or write operations. each 16-bit timer has a single 8-bit register for temporary storing of the high byte of the 16-bit access. the same temporary register is shared between all 16-bit registers within each 16-bit timer. accessing the low byte triggers the 16-bit read or write operation. when the low byte of a 16-bit register is written by the cpu, the high byte stored in the temporary register, and the low byte written are both copied into the 16-bit register in the same clock cycle. when the low byte of a 16-bit register is read by the cpu, the high byte of the 16-bit register is copied into the temporary register in the same clock cycle as the low byte is read. not all 16-bit accesses uses the temporary register for the high byte. reading the ocr1a/b 16-bit registers does not involve using the temporary register. to do a 16-bit write, the high byte must be written before the low byte. for a 16-bit read, the low byte must be read before the high byte. the following code examples show how to access the 16-bit timer registers assuming that no interrupts updates the temporary register. the same principle can be used directly for accessing the ocr1a/b and icr1 registers. note that when using ?c?, the compiler handles the 16-bit access. ocfnx ocrnx tcntn ocrnx value ocrnx - 1 ocrnx ocrnx + 1 ocrnx + 2 clk i/o clk tn (clk i/o /8)
94 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 note: see ?code examples? on page 7 . the assembly code example returns the tcnt1 value in the r17:r16 register pair. it is important to notice that accessing 16-bit registers are atomic operations. if an interrupt occurs between the two instructions accessing the 16-bit register, and the interrupt code updates the temporary register by accessing the same or any other of the 16-bit timer registers, then the result of the access outside the interrupt will be corrupted. therefore, when both the main code and the interrupt code update the temporary register, the main code must disable the interrupts during the 16-bit access. the following code examples show how to do an atomic read of the tcnt1 register contents. reading any of the ocr1a/b or icr1 registers can be done by using the same principle. assembly code example ... ; set tcnt 1 to 0x01ff ldi r17,0x01 ldi r16,0xff out tcnt 1 h,r17 out tcnt 1 l,r16 ; read tcnt 1 into r17:r16 in r16,tcnt 1 l in r17,tcnt 1 h ... c code example unsigned int i; ... /* set tcnt 1 to 0x01ff */ tcnt 1 = 0x1ff; /* read tcnt 1 into i */ i = tcnt 1 ; ... assembly code example tim16_readtcnt 1 : ; save global interrupt flag in r18,sreg ; disable interrupts cli ; read tcnt 1 into r17:r16 in r16,tcnt 1 l in r17,tcnt 1 h ; restore global interrupt flag out sreg,r18 ret
95 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 note: see ?code examples? on page 7 . the assembly code example returns the tcnt1 value in the r17:r16 register pair. the following code examples show how to do an atomic write of the tcnt1 register contents. writing any of the ocr1a/b or icr1 registers can be done by using the same principle. c code example unsigned int tim16_readtcnt 1 ( void ) { unsigned char sreg; unsigned int i; /* save global interrupt flag */ sreg = sreg; /* disable interrupts */ _cli(); /* read tcnt 1 into i */ i = tcnt 1 ; /* restore global interrupt flag */ sreg = sreg; return i; } assembly code example tim16_writetcnt 1 : ; save global interrupt flag in r18,sreg ; disable interrupts cli ; set tcnt 1 to r17:r16 out tcnt 1 h,r17 out tcnt 1 l,r16 ; restore global interrupt flag out sreg,r18 ret
96 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 note: see ?code examples? on page 7 . the assembly code example requires that the r17:r16 register pair contains the value to be written to tcnt1. 12.10.1 reusing the temporary high byte register if writing to more than one 16-bit register where the high byte is the same for all registers written, then the high byte only needs to be written once. however, note that the same rule of atomic operation described previously also applies in this case. 12.11 register description 12.11.1 tccr1a ? timer/counter1 control register a z bits 7:6 ? com1a[1:0]: compare output mode for channel a z bits 5:4 ? com1b[1:0]: compare output mode for channel b the com1a[1:0] and com1b[1:0] control the output compare pins (oc1a and oc1b respectively) behavior. if one or both of the com1a[1:0] bits are written to one, the oc1a output overrides the normal port functionality of the i/o pin it is connected to. if one or both of the com1b[1:0] bit are written to one, the oc1b output overrides the normal port functionality of the i/o pin it is connected to. however, note that the data direction register (ddr) bit corresponding to the oc1a or oc1b pin must be set in order to enable the output driver. when the oc1a or oc1b is connected to the pin, the function of the com1x[1:0] bits is dependent of the wgm1[3:0] bits setting. table 12-2 shows com1x[1:0] bit functionality when wgm1[3:0] bits are set to a normal or a ctc mode (non-pwm). c code example void tim16_writetcnt 1 ( unsigned int i ) { unsigned char sreg; unsigned int i; /* save global interrupt flag */ sreg = sreg; /* disable interrupts */ _cli(); /* set tcnt 1 to i */ tcnt 1 = i; /* restore global interrupt flag */ sreg = sreg; } bit 7 6 5 4 3210 0x24 com1a1 com1a0 com1b1 com1b0 ? ? wgm11 wgm10 tccr1a read/write r/w r/w r/w r/w r r r/w r/w initial value 0 0 0 0 0000
97 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 table 12-2. compare output mode, non-pwm table 12-3 shows com1x[1:0] bit functionality when wgm1[3:0] bits are set to fast pwm mode. table 12-3. compare output mode, fast pwm note: a special case occurs when ocr1a/ocr1b equals top and com1a1/com1b1 is set. in this case the com- pare match is ignored, but the set or clear is done at bottom. see ?fast pwm mode? on page 85 for more details. table 12-4 shows com1x[1:0] bit functionality when wgm1[3:0] bits are set to phase correct or phase and frequency correct pwm mode. table 12-4. compare output mode, phase correct and phase & frequency correct pwm note: a special case occurs when ocr1a/ocr1b equals top and com1a1/com1b1 is set. ?phase correct pwm mode? on page 87 for more details. com1a1 com1b1 com1a0 com1b0 description 0 0 normal port operation, oc1a/oc1b disconnected 0 1 toggle oc1a/oc1b on compare match 1 0 clear oc1a/oc1b on compare match (set output to low level) 1 1 set oc1a/oc1b on compare match (set output to high level). com1a1 com1b1 com1a0 com1b0 description 0 0 normal port operation, oc1a/oc1b disconnected 0 1 wgm13=0: normal port operation, oc1a/oc1b disconnected wgm13=1: toggle oc1a on compare match, oc1b reserved 1 0 clear oc1a/oc1b on compare match, set oc1a/oc1b at bottom (non- inverting mode) 1 1 set oc1a/oc1b on compare match, clear oc1a/oc1b at bottom (inverting mode) com1a1 com1b1 com1a0 com1b0 description 0 0 normal port operation, oc1a/oc1b disconnected 0 1 wgm13=0: normal port operation, oc1a/oc1b disconnected wgm13=1: toggle oc1a on compare match, oc1b reserved 1 0 clear oc1a/oc1b on compare match when up-counting set oc1a/oc1b on compare match when downcounting 1 1 set oc1a/oc1b on compare match when up-counting clear oc1a/oc1b on compare match when downcounting
98 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 z bits 1:0 ? wgm1[1:0]: waveform generation mode combined with the wgm1[3:2] bits found in the tccr1b register, these bits control the counting sequence of the counter, the source for maximum (top) counter value, and what type of waveform generation to be used, see table 12- 5 . modes of operation supported by the timer/counter unit are: normal mode (counter), clear timer on compare match (ctc) mode, and three types of pulse width modulation (pwm) modes. ( ?modes of operation? on page 83 ). table 12-5. waveform generation modes 12.11.2 tccr1b ? timer/counter1 control register b z bit 7 ? icnc1: input capture noise canceler setting this bit (to one) activates the input capture noise canceler. when the noise canceler is activated, the input from the input capture pin (icp1) is filtered. the filter function requires four successive equal valued samples of the icp1 pin for changing its output. the input capture is therefore delayed by four oscillator cycles when the noise canceler is enabled. mode wgm1[3:0] mode of operation top updateocr1 x at tov1 set on 0 0000 normal 0xffff immediate max 1 0001 pwm, phase correct, 8-bit 0x00ff top bottom 2 0010 pwm, phase correct, 9-bit 0x01ff top bottom 3 0011 pwm, phase correct, 10-bit 0x03ff top bottom 4 0100 ctc ( clear timer on compare ) ocr1a immediate max 5 0101 fast pwm, 8-bit 0x00ff top top 6 0110 fast pwm, 9-bit 0x01ff top top 7 0111 fast pwm, 10-bit 0x03ff top top 8 1000 pwm, phase & freq. correct icr1 bottom bottom 9 1001 pwm, phase & freq. correct ocr1a bottom bottom 10 1010 pwm, phase correct icr1 top bottom 11 1011 pwm, phase correct ocr1a top bottom 12 1100 ctc ( clear timer on compare ) icr1 immediate max 13 1101 (reserved) ? ? ? 14 1110 fast pwm icr1 top top 15 1111 fast pwm ocr1a top top bit 7654 3210 0x23 icnc1 ices1 ? wgm13 wgm12 cs12 cs11 cs10 tccr1b read/write r/w r/w r r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
99 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 z bit 6 ? ices1: input capture edge select this bit selects which edge on the input capture pin (icp1) that is used to trigger a capture event. when the ices1 bit is written to zero, a falling (negative) edge is used as trigger, and when the ices1 bit is written to one, a rising (positive) edge will trigger the capture. when a capture is triggered according to the ices1 setting, the counter value is copied into the input capture register (icr1). the event will also set the input capture flag (icf1), and this can be used to cause an input capture interrupt, if this interrupt is enabled. when the icr1 is used as top value (see description of the wgm1[3:0] bits located in the tccr1a and the tccr1b register), the icp1 is disconnected and consequently the input capture function is disabled. z bit 5 ? res: reserved bit this bit is reserved for future use. for ensuring compatibility with future devices, this bit must be written to zero when tccr1b is written. z bits 4:3 ? wgm1[3:2]: waveform generation mode see tccr1a register description. z bits 2:0 ? cs1[2:0]: clock select the three clock select bits select the clock source to be used by the timer/counter, see figure 12-10 on page 91 and figure 12-11 on page 92 . table 12-6. clock select bit description if external pin modes are used for the timer/counter1, transitions on the t1 pin will clock the counter even if the pin is configured as an output. this feature allows software control of the counting. cs12 cs11 cs10 description 0 0 0 no clock source (timer/counter stopped). 0 0 1 clk i/o /1 (no prescaling) 0 1 0 clk i/o /8 (from prescaler) 0 1 1 clk i/o /64 (from prescaler) 1 0 0 clk i/o /256 (from prescaler) 1 0 1 clk i/o /1024 (from prescaler) 1 1 0 external clock source on t1 pin. clock on falling edge. 1 1 1 external clock source on t1 pin. clock on rising edge.
100 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 12.11.3 tccr1c ? timer/counter1 control register c z bit 7 ? foc1a: force output compare for channel a z bit 6 ? foc1b: force output compare for channel b the foc1a/foc1b bits are only active when the wgm1[3:0] bits specifies a non-pwm mode. however, for ensuring compatibility with future devices, these bits must be set to zero when tccr1a is written when operating in a pwm mode. when writing a logical one to the foc1a/foc1b bit, an immediate compare match is forced on the waveform generation unit. the oc1a/oc1b output is changed according to its com1x[1:0] bits setting. note that the foc1a/foc1b bits are implemented as strobes. therefore it is the value present in the com1x[1:0] bits that determine the effect of the forced compare. a foc1a/foc1b strobe will not generate any interrupt nor will it clear the timer in clear timer on compare match (ctc) mode using ocr1a as top. the foc1a/foc1b bits are always read as zero. z bits 5:0 ? res: reserved bit these bits are reserved for future use. to ensure compatibility with future devices, these bits must be set to zero when the register is written. 12.11.4 tcnt1h and tcnt1l ? timer/counter1 the two timer/counter i/o locations (tcnt1h and tcnt1l, combined tcnt1) give direct access, both for read and for write operations, to the timer/counter unit 16-bit counter. to ensure that both the high and low bytes are read and written simultaneously when the cpu accesses these registers, the access is performed using an 8-bit temporary high byte register (temp). this temporary register is shared by all the other 16-bit registers. see ?accessing 16-bit registers? on page 93 . modifying the counter (tcnt1) while the counter is running introduces a risk of missing a compare match between tcnt1 and one of the ocr1x registers. writing to the tcnt1 register blocks (removes) the compare match on the following timer clock for all compare units. 12.11.5 ocr1ah and ocr1al ? output compare register 1 a bit 7654 3210 0x22 foc1a foc1b ? ? ? ? ? ? tccr1c read/write w w r r r r r r initial value 0 0 0 0 0 0 0 0 bit 76543210 0x21 tcnt1[15:8] tcnt1h 0x20 tcnt1[7:0] tcnt1l read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 00000000 bit 76543210 0x1f ocr1a[15:8] ocr1ah 0x1e ocr1a[7:0] ocr1al read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 00000000
101 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 12.11.6 ocr1bh and ocr1bl ? output compare register 1 b the output compare registers contain a 16-bit value that is continuously compared with the counter value (tcnt1). a match can be used to generate an output compare interrupt, or to generate a waveform output on the oc1x pin. the output compare registers are 16-bit in size. to ensure that both the high and low bytes are written simultaneously when the cpu writes to these registers, the access is performed using an 8-bit temporary high byte register (temp). this temporary register is shared by all the other 16-bit registers. see ?accessing 16-bit registers? on page 93 . 12.11.7 icr1h and icr1l ? input capture register 1 the input capture is updated with the counter (tcnt1) value each time an event occurs on the icp1 pin (or optionally on the analog comparator output for timer/counter1). the input capture can be used for defining the counter top value. the input capture register is 16-bit in size. to ensure that both the high and low bytes are read simultaneously when the cpu accesses these registers, the access is performed using an 8-bit temporary high byte register (temp). this temporary register is shared by all the other 16-bit registers. ?accessing 16-bit registers? on page 93 . 12.11.8 timsk ? timer/counter interrupt mask register z bit 7 ? icie1: timer/counter1, input capture interrupt enable when this bit is written to one, and the i-flag in the status register is set (interrupts globally enabled), the timer/counter input capture interrupt is enabled. the corresponding interrupt vector (see ?interrupts? on page 66.) is executed when the icf1 flag, located in tifr, is set. z bit 5 ? ocie1b: timer/counter1, output compare b match interrupt enable when this bit is written to one, and the i-flag in the status register is set (interrupts globally enabled), the timer/counter1 output compare b match interrupt is enabled. the corresponding interrupt vector (see ?interrupts? on page 36 ) is executed when the ocf1b flag, located in tifr, is set. z bit 4 ? ocie1a: timer/counter1, output compare a match interrupt enable when this bit is written to one, and the i-flag in the status register is set (interrupts globally enabled), the timer/counter1 output compare a match interrupt is enabled. the corresponding interrupt vector (see ?interrupts? on page 36 ) is executed when the ocf1a flag, located in tifr, is set. z bit 3 ? toie1: timer/counter1, overflow interrupt enable bit 76543210 0x1d ocr1b[15:8] ocr1bh 0x1c ocr1b[7:0] ocr1bl read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 00000000 bit 76543210 0x1b icr1[15:8] icr1h 0x1a icr1[7:0] icr1l read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 00000000 bit 76543210 0x26 icie1 ? ocie1b ocie1a toie1 ocie0b ocie0a toie0 timsk read/write r/w r r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
102 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 when this bit is written to one, and the i-flag in the status register is set (interrupts globally enabled), the timer/counter1 overflow interrupt is enabled. the corresponding interrupt vector (see ?interrupts? on page 36 ) is executed when the tov1 flag, located in tifr, is set. 12.11.9 tifr ? timer/counter interrupt flag register z bit 7 ? icf1: timer/counter1, input capture flag this flag is set when a capture event occurs on the icp1 pin. when the input capture register (icr1) is set by the wgm1[3:0] to be used as the top value, the icf1 flag is set when the counter reaches the top value. icf1 is automatically cleared when the input capture interrupt vector is executed. alternatively, icf1 can be cleared by writing a logic one to its bit location. z bit 5 ? ocf1b: timer/counter1, output compare b match flag this flag is set in the timer clock cycle after the counter (tcnt1) value matches the output compare register b (ocr1b). note that a forced output compare (1b) strobe will not set the ocf1b flag. ocf1b is automatically cleared when the output compare match b interrupt vector is executed. alternatively, ocf1b can be cleared by writing a logic one to its bit location. z bit 4 ? ocf1a: timer/counter1, output compare a match flag this flag is set in the timer clock cycle after the counter (tcnt1) value matches the output compare register a (ocr1a). note that a forced output compare (1a) strobe will not set the ocf1a flag. ocf1a is automatically cleared when the output compare match a interrupt vector is executed. alternatively, ocf1a can be cleared by writing a logic one to its bit location. z bit 3 ? tov1: timer/counter1, overflow flag the setting of this flag is dependent of the wgm1[3:0] bits setting. in normal and ctc modes, the tov1 flag is set when the timer overflows. see table 12-5 on page 98 for the tov1 flag behavior when using another wgm1[3:0] bit setting. tov1 is automatically cleared when the timer/counter1 overflow interrupt vector is executed. alternatively, tov1 can be cleared by writing a logic one to its bit location. bit 765432 1 0 0x25 icf1 ? ocf1b ocf1a tov1 ocf0b ocf0a tov0 tifr read/write r/w r r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
103 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 13. timer/counter prescaler timer/counter0 and timer/counter1 share the same prescaler module, but the timer/counters can have different prescaler settings. the description below applies to both timer/counters. tn is used as a general name, n = 0, 1. the timer/counter can be clocked directly by the system clock (by setting the csn[2:0] = 1). this provides the fastest operation, with a maximum timer/counter clock frequency equal to system clock frequency (f clk_i/o ). alternatively, one of four taps from the prescaler can be used as a clock source. the prescaled clock has a frequency of either f clk_i/o /8, f clk_i/o /64, f clk_i/o /256, or f clk_i/o /1024. 13.1 prescaler reset the prescaler is free running, i.e., operates independently of the clock select logic of the timer/counter, and it is shared by the timer/counter tn. since the prescaler is not affected by the timer/counter?s clock select, the state of the prescaler will have implications for situations where a prescaled clock is used. one example of prescaling artifacts occurs when the timer is enabled and clocked by the prescaler (csn[2:0] = 2, 3, 4, or 5). the number of system clock cycles from when the timer is enabled to the first count occurs can be from 1 to n+1 system clock cycles, where n equals the prescaler divisor (8, 64, 256, or 1024). it is possible to use the prescaler reset for synchronizing the timer/counter to program execution. 13.2 external clock source an external clock source applied to the tn pin can be used as timer/counter clock (clk tn ). the tn pin is sampled once every system clock cycle by the pin synchronization logic. the synchronized (sampled) signal is then passed through the edge detector. figure 13-1 shows a functional equivalent block diagram of the tn synchronization and edge detector logic. the registers are clocked at the positive edge of the internal system clock ( clk i/o ). the latch is transparent in the high period of the internal system clock. the edge detector generates one clk t 0 pulse for each positive (csn[2:0] = 7) or negative (csn[2:0] = 6) edge it detects. figure 13-1. t0 pin sampling the synchronization and edge detector logic introduces a delay of 2.5 to 3.5 system clock cycles from an edge has been applied to the tn pin to the counter is updated. enabling and disabling of the clock input must be done when tn has been stable for at least one system clock cycle, otherwise it is a risk that a false timer/counter clock pulse is generated. each half period of the external clock applied must be longer than one system clock cycle to ensure correct sampling. the external clock must be guaranteed to have less than half the system clock frequency (f extclk < f clk_i/o /2) given a 50/50% duty cycle. since the edge detector uses sampling, the maximum frequency of an external clock it can detect is half the sampling frequency (nyquist sampling theorem). however, due to variation of the system clock frequency and duty cycle caused by oscillator source (crystal, resonator, and capacitors) tolerances, it is recommended that maximum frequency of an external clock source is less than f clk_i/o /2.5. an external clock source can not be prescaled. tn_sync (to clock s elect logic) edge detector s ynchroniz a tion dq dq le dq tn clk i/o
104 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 13-2. prescaler for timer/counter0 note: 1. the synchronization logic on the input pins ( t0) is shown in figure 13-1 on page 103 . 13.3 register description 13.3.1 gtccr ? general timer/counter control register z bit 7 ? tsm: timer/counter synchronization mode writing the tsm bit to one activates the timer/counter synchronization mode. in this mode, the value that is written to the psr bit is kept, hence keeping the prescaler reset signal asserted. this ensures that the timer/counter is halted and can be configured without the risk of advancing during configuration. when the tsm bit is written to zero, the psr bit is cleared by hardware, and the timer/counter start counting. z bit 0 ? psr: prescaler reset when this bit is one, the timer/counter prescaler will be reset. this bit is normally cleared immediately by hardware, except if the tsm bit is set. p s r cle a r clk t0 t0 clk i/o s ynchroniz a tion bit 7 6 5 4 3 2 1 0 0x27 tsm ? ? ? ? ? ? psr gtccr read/write r/w r r r r r r r/w initial value 0 0 0 0 0 0 0 0
105 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 14. analog comparator the analog comparator compares the input values on the positive pin ain0 and negative pin ain1. when the voltage on the positive pin ain0 is higher than the voltage on the negative pin ain1, the analog comparator output, aco, is set. the comparator can trigger a separate interrupt, exclusive to the analog comparator. the user can select interrupt triggering on comparator output rise, fall or toggle. a block diagram of the comparator and its surrounding logic is shown in figure 14-1 . figure 14-1. analog comparator block diagram note: 1. see table 14-1 . see figure 1-1 on page 2 and table 10-9 on page 56 for analog comparator pin placement. the adc power reduction bit, pradc, must be disabled in order to use the adc input multiplexer. this is done by clearing the pradc bit in the power reduction register, prr. see ?prr ? power reduction register? on page 27 for more details. 14.1 analog comparator multiplexed input when the analog to digital converter (adc) is configurated as single ended input channel, it is possible to select any of the adc[7:0] pins to replace the negative input to the analog comparator. the adc multiplexer is used to select this input. if the analog comparator multiplexer enable bit (acme in adcsrb) is set, mux bits in admux select the input pin to replace the negative input to the analog comparator. table 14-1. analog comparator multiplexed input acbg aco bandgap reference adc multiplexer output to t/c1 capture trigger mux acic acme (1) hsel hlev acme mux[3:0] analog comparator negative input 0 xxxx ain1 1 0000 adc0 1 0001 adc1
106 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 14.2 register description 14.2.1 acsra ? analog comparator control and status register z bit 7 ? acd: analog comparator disable when this bit is written logic one, the power to the analog comparator is switched off. this bit can be set at any time to turn off the analog comparator. this will reduce power consumption in active and idle mode. when changing the acd bit, the analog comparator interrupt must be disabled by clearing the acie bit in acsr. otherwise an interrupt can occur when the bit is changed. z bit 6 ? acbg: analog comparator bandgap select when this bit is set, a fixed, internal bandgap reference voltage replaces the positive input to the analog comparator. when this bit is cleared, ain0 is applied to the positive input of the analog comparator. z bit 5 ? aco: analog comparator output the output of the analog comparator is synchronized and then directly connected to aco. the synchronization introduces a delay of 1 - 2 clock cycles. z bit 4 ? aci: analog comparator interrupt flag this bit is set by hardware when a comparator output event triggers the interrupt mode defined by acis1 and acis0. the analog comparator interrupt routine is executed if the acie bit is set and the i-bit in sreg is set. aci is cleared by hardware when executing the corresponding interrupt handling vector. alternatively, aci is cleared by writing a logic one to the flag. z bit 3 ? acie: analog comparator interrupt enable when the acie bit is written logic one and the i-bit in the status register is set, the analog comparator interrupt is activated. when written logic zero, the interrupt is disabled. z bit 2 ? acic: analog comparator input capture enable when written logic one, this bit enables the input capture function in timer/counter1 to be triggered by the analog comparator. the comparator output is then directly connected to the input capture front-end logic, making the comparator utilize the noise canceler and edge select features of the timer/counter1 input capture interrupt. 1 0010 adc2 1 0011 adc3 1 0100 adc4 1 0101 adc5 1 0110 adc6 1 0111 adc7 acme mux[3:0] analog comparator negative input bit 76543210 0x14 acd acbg aco aci acie acic acis1 acis0 acsra read/write r/w r/w r r/w r/w r/w r/w r/w initial value 0 0 n/a 00000
107 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 when written logic zero, no connection between the analog comparator and the input capture function exists. to make the comparator trigger the timer/counter1 input capture interrupt, the icie1 bit in the timer interrupt mask register (timsk) must be set. z bits 1:0 ? acis[1:0]: analog comparator interrupt mode select these bits determine which comparator events that trigger the analog comparator interrupt. the different settings are shown in table 14-2 . table 14-2. acis1/acis0 settings when changing the acis1/acis0 bits, the analog comparator interrupt must be disabled by clearing its interrupt enable bit in the acsr register. otherwise an interrupt can occur when the bits are changed. 14.2.2 acsrb ? analog comparator control and status register b z bit 7 ? hsel: hysteresis select when this bit is written logic one, the hysteresis of the analog comparator is enabled. the level of hysteresis is selected by the hlev bit. z bit 6 ? hlev: hysteresis level when enabled via the hsel bit, the level of hysteresis can be set using the hlev bit, as shown in table 14-3 . table 14-3. selecting level of analog comparator hysteresis z bit 5 ? aclp this bit is reserved for qtouch, always write as zero. z bit 4 ? res: reserved bit this bit is reserved and will always read as zero. acis1 acis0 interrupt mode 0 0 comparator interrupt on output toggle. 0 1 reserved 1 0 comparator interrupt on falling output edge. 1 1 comparator interrupt on rising output edge. bit 7 6543210 0x13 hsel hlev aclp ? acce acme acirs1 acirs0 acsrb read/write r/w r/w r/w r r/w r/w r/w r/w initial value 0 0 0 00000 hsel hlev hysteresis of analog comparator 0 x not enabled 1 0 20 mv 1 50 mv
108 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 z bit 3 ? acce this bit is reserved for qtouch, always write as zero. z bit 2 ? acme: analog comparator multiplexer enable when this bit is written logic one and the adc is switched off (aden in adcsra is zero), the adc multiplexer selects the negative input to the analog comparator. when this bit is written logic zero, ain1 is applied to the negative input of the analog comparator. for a detailed description of this bit, see ?analog comparator multiplexed input? on page 105 . z bit 1 ? acirs1 this bit is reserved for qtouch, always write as zero. z bit 0 ? acirs0 this bit is reserved for qtouch, always write as zero. 14.2.3 didr0 ? digital input disable register 0 z bits 2:1 ? adc2d, adc1d: adc 2/1 digital input buffer disable when this bit is written logic one, the digital input buffer on the ain1/0 pin is disabled. the corresponding pin register bit will always read as zero when this bit is set. when used as an analog input but not required as a digital input the power consumption in the digital input buffer can be reduced by writing this bit to logic one. bit 76543210 0x0d adc7d adc6d adc5d adc4d adc3d adc2d adc1d adc0d didr0 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 000000
109 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 15. analog to digital converter 15.1 features z 10-bit resolution z 1 lsb integral non-linearity z 2 lsb absolute accuracy z 13 s conversion time z 15 ksps at maximum resolution z eight multiplexed single ended input channels z temperature sensor input channel z optional left adjustment for adc result readout z 0 - v cc adc input voltage range z 1.1v adc reference voltage z free running or single conversion mode z adc start conversion by auto triggering on interrupt sources z interrupt on adc conversion complete z sleep mode noise canceler 15.2 overview attiny20 features a 10-bit, successive approximation analog-to-digital converter (adc). the adc is wired to a nine- channel analog multiplexer, which allows the adc to measure the voltage at eight single-ended input pins, or from one internal, single-ended voltage channel coming from the internal temperature sensor. voltage inputs are referred to 0v (gnd). the adc contains a sample and hold circuit which ensures that the input voltage to the adc is held at a constant level during conversion. a block diagram of the adc is shown in figure 15-1 on page 110 . internal reference voltage of nominally 1.1v is provided on-chip. alternatively, v cc can be used as reference voltage for single ended channels.
110 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 15-1. analog to digital converter block schematic 15.3 operation in order to be able to use the adc the power reduction bit, pradc, in the power reduction register must be disabled. this is done by clearing the pradc bit. see ?prr ? power reduction register? on page 27 for more details. admux a d m u x decoder d e c o d e r 8-bit data bus mux[3:0] adcsra a d c s r a adch+adcl a d c h + a d c l adps0 aden adps1 adps2 adate adif adsc trigger t r i g g e r select s e l e c t adts[2:0] start prescaler p r e s c a l e r adc9:0 conversion logic c o n v e r s i o n l o g i c 10-bit dac 1 0 - b i t d a c adcsrb a d c s r b - + adc3 a d c 3 adc2 a d c 2 adc1 a d c 1 adc0 a d c 0 v cc c c input i n p u t mux m u x channel adie adc irq a d c i r q sample & hold s a m p l e & h o l d comparator c o m p a r a t o r internal i n t e r n a l reference r e f e r e n c e temperature t e m p e r a t u r e sensor s e n s o r agnd adc mux output a d c m u x o u t p u t refs interrupt flags adlar adc7 a d c 7 adc6 a d c 6 adc5 a d c 5 adc4 a d c 4
111 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 the adc is enabled by setting the adc enable bit, aden in adcsra. voltage reference and input channel selections will not go into effect until aden is set. the adc does not consume power when aden is cleared, so it is recommended to switch off the adc before entering power saving sleep modes. the adc converts an analog input voltage to a 10-bit digital value using successive approximation. the minimum value represents gnd and the maximum value represents the reference voltage. the adc voltage reference is selected by writing the refs bit in the admux register. alternatives are the v cc supply pin and the internal 1.1v voltage reference. the analog input channel is selected by writing to the mux bits in admux. any of the adc input pins can be selected as single ended inputs to the adc. the adc generates a 10-bit result which is presented in the adc data registers, adch and adcl. by default, the result is presented right adjusted, but can optionally be presented left adjusted by setting the adlar bit in adcsrb. if the result is left adjusted and no more than 8-bit precision is required, it is sufficient to read adch, only. otherwise, adcl must be read first, then adch, to ensure that the content of the data registers belongs to the same conversion. once adcl is read, adc access to data registers is blocked. this means that if adcl has been read, and a conversion completes before adch is read, neither register is updated and the result from the conversion is lost. when adch is read, adc access to the adch and adcl registers is re-enabled. the adc has its own interrupt which can be triggered when a conversion completes. when adc access to the data registers is prohibited between reading of adch and adcl, the interrupt will trigger even if the result is lost. 15.4 starting a conversion make sure the adc is powered by clearing the adc power reduction bit, pradc, in the power reduction register, prr (see ?prr ? power reduction register? on page 27 ). a single conversion is started by writing a logical one to the adc start conversion bit, adsc. this bit stays high as long as the conversion is in progress and will be cleared by hardware when the conversion is completed. if a different data channel is selected while a conversion is in progress, the adc will finish the current conversion before performing the channel change. alternatively, a conversion can be triggered automatically by various sources. auto triggering is enabled by setting the adc auto trigger enable bit, adate in adcsra. the trigger source is selected by setting the adc trigger select bits, adts in adcsrb (see description of the adts bits for a list of the trigger sources). when a positive edge occurs on the selected trigger signal, the adc prescaler is reset and a conversion is started. this provides a method of starting conversions at fixed intervals. if the trigger signal still is set when the conversion completes, a new conversion will not be started. if another positive edge occurs on the trigger signal during conversion, the edge will be ignored. note that an interrupt flag will be set even if the specific interrupt is disabled or the global interrupt enable bit in sreg is cleared. a conversion can thus be triggered without causing an interrupt. however, the interrupt flag must be cleared in order to trigger a new conversion at the next interrupt event. figure 15-2. adc auto trigger logic adsc adif source 1 source n adts[2:0] conversion logic prescaler start clk adc . . . . edge detector adate
112 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 using the adc interrupt flag as a trigger source makes the adc start a new conversion as soon as the ongoing conversion has finished. the adc then operates in free running mode, constantly sampling and updating the adc data register. the first conversion must be started by writing a logical one to the adsc bit in adcsra. in this mode the adc will perform successive conversions independently of whether the adc interrupt flag, adif is cleared or not. if auto triggering is enabled, single conversions can be started by writing adsc in adcsra to one. adsc can also be used to determine if a conversion is in progress. the adsc bit will be read as one during a conversion, independently of how the conversion was started. 15.5 prescaling and conversion timing by default, the successive approximation circuitry requires an input clock frequency between 50 khz and 200 khz to get maximum resolution. if a lower resolution than 10 bits is needed, the input clock frequency to the adc can be higher than 200 khz to get a higher sample rate. it is not recommended to use a higher input clock frequency than 1 mhz. figure 15-3. adc prescaler the adc module contains a prescaler, as illustrated in figure 15-3 on page 112 , which generates an acceptable adc clock frequency from any cpu frequency above 100 khz. the prescaling is set by the adps bits in adcsra. the prescaler starts counting from the moment the adc is switched on by setting the aden bit in adcsra. the prescaler keeps running for as long as the aden bit is set, and is continuously reset when aden is low. when initiating a single ended conversion by setting the adsc bit in adcsra, the conversion starts at the following rising edge of the adc clock cycle. a normal conversion takes 13 adc clock cycles, as summarized in table 15-1 on page 115 . the first conversion after the adc is switched on (aden in adcsra is set) takes 25 adc clock cycles in order to initialize the analog circuitry, as shown in figure 15-4 below. 7-bit adc prescaler adc clock source ck adps0 adps1 adps2 ck/128 ck/2 ck/4 ck/8 ck/16 ck/32 ck/64 reset aden start
113 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 15-4. adc timing diagram, first conversion (single conversion mode) the actual sample-and-hold takes place 1.5 adc clock cycles after the start of a normal conversion and 13.5 adc clock cycles after the start of a first conversion. see figure 15-5 . when a conversion is complete, the result is written to the adc data registers, and adif is set. in single conversion mode, adsc is cleared simultaneously. the software may then set adsc again, and a new conversion will be initiated on the first rising adc clock edge. figure 15-5. adc timing diagram, single conversion when auto triggering is used, the prescaler is reset when the trigger event occurs, as shown in figure 15-6 below. this assures a fixed delay from the trigger event to the start of conversion. in this mode, the sample-and-hold takes place two adc clock cycles after the rising edge on the trigger source signal. three additional cpu clock cycles are used for synchronization logic. sign and msb of result lsb of result adc clock adsc sample & hold adif adch adcl cycle number aden 1 212 13 14 15 16 17 18 19 20 21 22 23 24 25 1 2 first conversion next conversion 3 mux and refs update mux and refs update conversion complete 1 2 3 4 5 6 7 8 9 10 11 12 13 sign and msb of result lsb of result adc clock adsc adif adch adcl cycle number 12 one conversion next conversion 3 sample & hold mux and refs update conversion complete mux and refs update
114 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 15-6. adc timing diagram, auto triggered conversion in free running mode, a new conversion will be started immediately after the conversion completes, while adsc remains high. see figure 15-7 . figure 15-7. adc timing diagram, free running conversion for a summary of conversion times, see table 15-1 . 1 2 3 4 5 6 7 8 9 10 11 12 13 sign and msb of result lsb of result adc clock trigger source adif adch adcl cycle number 12 one conversion next conversion conversion complete prescaler reset adate prescaler reset sample & hold mux and refs update 12 13 14 sign and msb of result lsb of result a dc clock a dsc a dif a dch a dcl c ycle number 12 one conversion next conversion 34 conversion complete sample & ho ld mux and refs update
115 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 table 15-1. adc conversion time 15.6 changing channel or reference selection the mux and refs bits in the admux register are single buffered through a temporary register to which the cpu has random access. this ensures that the channels and reference selection only takes place at a safe point during the conversion. the channel and reference selection is continuously updated until a conversion is started. once the conversion starts, the channel and reference selection is locked to ensure a sufficient sampling time for the adc. continuous updating resumes in the last adc clock cycle before the conversion completes (adif in adcsra is set). note that the conversion starts on the following rising adc clock edge after adsc is written. the user is thus advised not to write new channel or reference selection values to admux until one adc clock cycle after adsc is written. if auto triggering is used, the exact time of the triggering event can be indeterministic. special care must be taken when updating the admux register, in order to control which conversion will be affected by the new settings. if both adate and aden is written to one, an interrupt event can occur at any time. if the admux register is changed in this period, the user cannot tell if the next conversion is based on the old or the new settings. admux can be safely updated in the following ways: z when adate or aden is cleared. z during conversion, minimum one adc clock cycle after the trigger event. z after a conversion, before the interrupt flag used as trigger source is cleared. when updating admux in one of these conditions, the new settings will affect the next adc conversion. 15.6.1 adc input channels when changing channel selections, the user should observe the following guidelines to ensure that the correct channel is selected: z in single conversion mode, always select the channel before starting the conversion. the channel selection may be changed one adc clock cycle after writing one to adsc. however, the simplest method is to wait for the conversion to complete before changing the channel selection. z in free running mode, always select the channel before starting the first conversion. the channel selection may be changed one adc clock cycle after writing one to adsc. however, the simplest method is to wait for the first conversion to complete, and then change the channel selection. since the next conversion has already started automatically, the next result will reflect the previous channel selection. subsequent conversions will reflect the new channel selection. 15.6.2 adc voltage reference the adc reference voltage (v ref ) indicates the conversion range for the adc. single ended channels that exceed v ref will result in codes close to 0x3ff. v ref can be selected as either v cc , or internal 1.1v reference. the internal 1.1v reference is generated from the internal bandgap reference (v bg ) through an internal amplifier. condition sample & hold (cycles from start of conversion) conversion time (cycles) first conversion 13.5 25 normal conversions 1.5 13 auto triggered conversions 2 13.5 free running conversion 2.5 14
116 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 the first adc conversion result after switching reference voltage source may be inaccurate, and the user is advised to discard this result. 15.7 adc noise canceler the adc features a noise canceler that enables conversion during sleep mode. this reduces noise induced from the cpu core and other i/o peripherals. the noise canceler can be used with adc noise reduction and idle mode. to make use of this feature, the following procedure should be used: z make sure that the adc is enabled and is not busy converting. single conversion mode must be selected and the adc conversion complete interrupt must be enabled. z enter adc noise reduction mode (or idle mode). the adc will start a conversion once the cpu has been halted. z if no other interrupts occur before the adc conversion completes, the adc interrupt will wake up the cpu and execute the adc conversion complete interrupt routine. if another interrupt wakes up the cpu before the adc conversion is complete, that interrupt will be executed, and an adc conversion complete interrupt request will be generated when the adc conversion completes. the cpu will remain in active mode until a new sleep command is executed. note that the adc will not automatically be turned off when entering other sleep modes than idle mode and adc noise reduction mode. the user is advised to write zero to aden before entering such sleep modes to avoid excessive power consumption. 15.8 analog input circuitry the analog input circuitry for single ended channels is illustrated in figure 15-8 . an analog source applied to adcn is subjected to the pin capacitance and input leakage of that pin, regardless of whether that channel is selected as input for the adc. when the channel is selected, the source must drive the s/h capacitor through the series resistance (combined resistance in the input path). the adc is optimized for analog signals with an output impedance of approximately 10k or less. if such a source is used, the sampling time will be negligible. if a source with higher impedance is used, the sampling time will depend on how long time the source needs to charge the s/h capacitor, which can vary widely. the user is recommended to only use low impedance sources with slowly varying signals, since this minimizes the required charge transfer to the s/h capacitor. in order to avoid distortion from unpredictable signal convolution, signal components higher than the nyquist frequency (f adc /2) should not be present. the user is advised to remove high frequency components with a low-pass filter before applying the signals as inputs to the adc. figure 15-8. analog input circuitry note: the capacitor in the figure depicts the total capacitance, including the sample/hold capacitor and any stray or parasitic capacitance inside the device. the value given is worst case. adcn i ih 1..100 k ohm c s /h = 14 pf v cc /2 i il
117 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 15.9 noise canceling techniques digital circuitry inside and outside the device generates emi which might affect the accuracy of analog measurements. when conversion accuracy is critical, the noise level can be reduced by applying the following techniques: z keep analog signal paths as short as possible. z make sure analog tracks run over the analog ground plane. z keep analog tracks well away from high-speed switching digital tracks. z if any port pin is used as a digital output, it mustn?t switch while a conversion is in progress. z place bypass capacitors as close to v cc and gnd pins as possible. where high adc accuracy is required it is recommended to use adc noise reduction mode, as described in section 15.7 on page 116 . this is especially the case when system clock frequency is above 1 mhz, or when the adc is used for reading the internal temperature sensor, as described in section 15.12 on page 119 . a good system design with properly placed, external bypass capacitors does reduce the need for using adc noise reduction mode 15.10 adc accuracy definitions an n-bit single-ended adc converts a voltage linearly between gnd and v ref in 2 n steps (lsbs). the lowest code is read as 0, and the highest code is read as 2 n -1. several parameters describe the deviation from the ideal behavior, as follows: z offset: the deviation of the first transition (0x000 to 0x001) compared to the ideal transition (at 0.5 lsb). ideal value: 0 lsb. figure 15-9. offset error output code v ref input voltage ideal adc actual adc offset error
118 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 z gain error: after adjusting for offset, the gain error is found as the deviation of the last transition (0x3fe to 0x3ff) compared to the ideal transition (at 1.5 lsb below maximum). ideal value: 0 lsb figure 15-10.gain error z integral non-linearity (inl): after adjusting for offset and gain error, the inl is the maximum deviation of an actual transition compared to an ideal transition for any code. ideal value: 0 lsb. figure 15-11.integral non-linearity (inl) output code v ref input voltage ideal adc actual adc gain error output code v ref input voltage ideal adc actual adc inl
119 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 z differential non-linearity (dnl): the maximum deviation of the actual code width (the interval between two adjacent transitions) from the ideal code width (1 lsb). ideal value: 0 lsb. figure 15-12.differential non-linearity (dnl) z quantization error: due to the quantization of the input voltage into a finite number of codes, a range of input voltages (1 lsb wide) will code to the same value. always 0.5 lsb. z absolute accuracy: the maximum deviation of an actual (unadjusted) transition compared to an ideal transition for any code. this is the compound effect of offset, gain error, differential error, non-linearity, and quantization error. ideal value: 0.5 lsb. 15.11 adc conversion result after the conversion is complete (adif is high), the conversion result can be found in the adc data registers (adcl, adch). the result is, as follows: where v in is the voltage on the selected input pin and v ref the selected voltage reference (see table 15-3 on page 120 and table 15-4 on page 121 ). 0x000 represents analog ground, and 0x3ff represents the selected reference voltage minus one lsb. the result is presented in one-sided form, from 0x3ff to 0x000. 15.12 temperature measurement the temperature measurement is based on an on-chip temperature sensor that is coupled to a single ended adc channel. the temperature sensor is measured via channel adc8 and is enabled by writing mux bits in admux register to ?1010?. the internal 1.1v reference must also be selected for the adc reference source in the temperature sensor measurement. when the temperature sensor is enabled, the adc converter can be used in single conversion mode to measure the voltage over the temperature sensor. the measured voltage has a linear relationship to the temperature as described in table 15-2 the sensitivity is approximately 1 lsb / c and the accuracy depends on the method of user calibration. typically, the measurement output code 0xff 0x00 0 v ref input voltage dnl 1 lsb adc v in 1024 ? v ref -------------------------- =
120 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 accuracy after a single temperature calibration is 10 c, assuming calibration at room temperature. better accuracies are achieved by using two temperature points for calibration. table 15-2. temperature vs. sensor output voltage (typical case) the values described in table 15-2 are typical values. however, due to process variation the temperature sensor output voltage varies from one chip to another. to be capable of achieving more accurate results the temperature measurement can be calibrated in the application software. the software calibration can be done using the formula: t = k * [(adch << 8) | adcl] + t os where adch and adcl are the adc data registers, k is the fixed slope coefficient and t os is the temperature sensor offset. typically, k is very close to 1.0 and in single-point calibration the coefficient may be omitted. where higher accuracy is required the slope coefficient should be evaluated based on measurements at two temperatures. 15.13 register description 15.13.1 admux ? adc multiplexer selection register z bit 7 ? res: reserved bit this bit is reserved and will always read as zero. z bit 6 ? refs: reference selection bit this bits selects the voltage reference for the adc, as shown in table 15-3 . table 15-3. voltage reference selections for adc if this bit is changed during a conversion, the change will not go in effect until this conversion is complete (adif in adcsr is set). also note, that when these bits are changed, the next conversion will take 25 adc clock cycles. z bit 5 ? refen this bit is reserved for qtouch, always write as zero. z bit 4 ? adc0en this bit is reserved for qtouch, always write as zero. temperature -40 c +25 c +85 c adc 235 lsb 300 lsb 360 lsb bit 7 6 5 4 3 2 1 0 0x10 ? refs refen adc0en mux3 mux2 mux1 mux0 admux read/write r r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 refs voltage reference selection 0 v cc used as analog reference 1 internal 1.1v voltage reference
121 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 z bits 3:0 ? mux[3:0]: analog channel and gain selection bits the value of these bits selects which analog input is connected to the adc, as shown in table 15-4 . selecting channel adc8 enables temperature measurement. table 15-4. single-ended input channel selections notes: 1. after switching to internal voltage reference the adc requires a settling time of 1ms before measurements are stable. conversions starting before this may not be reliable. the adc must be enabled during the set- tling time. 2. see ?temperature measurement? on page 119 . if these bits are changed during a conversion, the change will not go into effect until this conversion is complete (adif in adcsra is set). 15.13.2 adcl and adch ? adc data register 15.13.2.1adlar = 0 single ended input mux[3:0] adc0 (pa0) 0000 adc1 (pa1) 0001 adc2 (pa2) 0010 adc3 (pa3) 0011 adc4 (pa4) 0100 adc5 (pa5) 0101 adc6 (pa6) 0110 adc7 (pa7) 0111 0v (agnd) 1000 internal 1.1v voltage reference (1) 1001 adc8 (temperature sensor) (2) 1010 reserved 1011 ? 1111 bit 15 14 13 12 11 10 9 8 0x0f ? ? ? ? ? ? adc9 adc8 adch 0x0e adc7 adc6 adc5 adc4 adc3 adc2 adc1 adc0 adcl 76543210 read/write r r r r r r r r rrrrrrrr initial value 0 0 0 0 0 0 0 0 00000000
122 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 15.13.2.2adlar = 1 when an adc conversion is complete, the result is found in these two registers. when adcl is read, the adc data register is not updated until adch is read. consequently, if the result is left adjusted and no more than 8-bit precision is required, it is sufficient to read adch. otherwise, adcl must be read first, then adch. the adlar bit in adcsrb, and the mux bits in admux affect the way the result is read from the registers. if adlar is set, the result is left adjusted. if adlar is cleared (default), the result is right adjusted. z adc[9:0]: adc conversion result these bits represent the result from the conversion, as detailed in ?adc conversion result? on page 119 . 15.13.3 adcsra ? adc control and status register a z bit 7 ? aden: adc enable writing this bit to one enables the adc. by writing it to zero, the adc is turned off. turning the adc off while a conversion is in progress, will terminate this conversion. z bit 6 ? adsc: adc start conversion in single conversion mode, write this bit to one to start each conversion. in free running mode, write this bit to one to start the first conversion. the first conversion after adsc has been written after the adc has been enabled, or if adsc is written at the same time as the adc is enabled, will take 25 adc clock cycles instead of the normal 13. this first conversion performs initialization of the adc. adsc will read as one as long as a conversion is in progress. when the conversion is complete, it returns to zero. writing zero to this bit has no effect. z bit 5 ? adate: adc auto trigger enable when this bit is written to one, auto triggering of the adc is enabled. the adc will start a conversion on a positive edge of the selected trigger signal. the trigger source is selected by setting the adc trigger select bits, adts in adcsrb. z bit 4 ? adif: adc interrupt flag this bit is set when an adc conversion completes and the data registers are updated. the adc conversion complete interrupt is executed if the adie bit and the i-bit in sreg are set. adif is cleared by hardware when executing the corresponding interrupt handling vector. alternatively, adif is cleared by writing a logical one to the flag. beware that if doing a read-modify-write on adcsra, a pending interrupt can be disabled. bit 15 14 13 12 11 10 9 8 0x0f adc9 adc8 adc7 adc6 adc5 adc4 adc3 adc2 adch 0x0e adc1 adc0 ? ? ? ? ? ? adcl 76543210 read/write r r r r r r r r rrrrrrrr initial value 0 0 0 0 0 0 0 0 00000000 bit 76543210 0x12 aden adsc adate adif adie adps2 adps1 adps0 adcsra read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 00000000
123 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 z bit 3 ? adie: adc interrupt enable when this bit is written to one and the i-bit in sreg is set, the adc conversion complete interrupt is activated. z bits 2:0 ? adps[2:0]: adc prescaler select bits these bits determine the division factor between the system clock frequency and the input clock to the adc. table 15-5. adc prescaler selections 15.13.4 adcsrb ? adc control and status register b z bit 7 ? vden this bit is reserved for qtouch, always write as zero. z bit 6 ? vdpd this bit is reserved for qtouch, always write as zero. z bits 5:4 ? res: reserved bits these are reserved bits. for compatibility with future devices always write these bits to zero. z bit 3 ? adlar: adc left adjust result the adlar bit affects the presentation of the adc conversion result in the adc data register. write one to adlar to left adjust the result. otherwise, the result is right adjusted. changing the adlar bit will affect the adc data register immediately, regardless of any ongoing conversions. for a complete description of this bit, see ?adcl and adch ? adc data register? on page 121 . z bits 2:0 ? adts[2:0]: adc auto trigger source if adate in adcsra is written to one, the value of these bits selects which source will trigger an adc conversion. if adate is cleared, the adts[2:0] settings will have no effect. a conversion will be triggered by the rising edge of the adps2 adps1 adps0 division factor 0 0 0 2 0 0 1 2 0 1 0 4 0 1 1 8 1 0 0 16 1 0 1 32 1 1 0 64 1 1 1 128 bit 7 6543210 0x11 vden vdpd ? ? adlar adts2 adts1 adts0 adcsrb read/write r/w r/w r r r/w r/w r/w r/w initial value 0 0 0 0 0000
124 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 selected interrupt flag. note that switching from a trigger source that is cleared to a trigger source that is set, will generate a positive edge on the trigger signal. if aden in adcsra is set, this will start a conversion. switching to free running mode (adts[2:0]=0) will not cause a trigger event, even if the adc interrupt flag is set . table 15-6. adc auto trigger source selections 15.13.5 didr0 ? digital input disable register 0 z bits 7:0 ? adc7d:adc0d: adc[7:0] digital input disable when a bit is written logic one, the digital input buffer on the corresponding adc pin is disabled. the corresponding pin register bit will always read as zero when this bit is set. when an analog signal is applied to the adc[7:0] pin and the digital input from this pin is not needed, this bit should be written logic one to reduce power consumption in the digital input buffer. adts2 adts1 adts0 trigger source 0 0 0 free running mode 0 0 1 analog comparator 0 1 0 external interrupt request 0 0 1 1 timer/counter0 compare match a 1 0 0 timer/counter0 overflow 1 0 1 timer/counter1 compare match b 1 1 0 timer/counter1 overflow 1 1 1 timer/counter1 capture event bit 76543210 0x0d adc7d adc6d adc5d adc4d adc3d adc2d adc1d adc0d didr0 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 00000000
125 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 16. spi ? serial peripheral interface 16.1 features z full-duplex, three-wire synchronous data transfer z master or slave operation z lsb first or msb first data transfer z seven programmable bit rates z end of transmission interrupt flag z write collision flag protection z wake-up from idle mode z double speed (ck/2) master spi mode 16.2 overview the serial peripheral interface (spi) allows high-speed synchronous data transfer between the attiny20 and peripheral devices or between several avr devices. the spi module is illustrated in figure 16-1 .
126 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 16-1. spi block diagram note: refer to figure 1-1 on page 2 , and table 16-1 on page 127 for spi pin placement. to enable the spi module, the prspi bit in the power reduction register must be written to zero. see ?prr ? power reduction register? on page 27 . the interconnection between master and slave cpus with spi is shown in figure 16-2 on page 127 . the system consists of two shift registers, and a master clock generator. the spi master initiates the communication cycle when pulling low the slave select ss pin of the desired slave. master and slave prepare the data to be sent in their respective shift registers, and the master generates the required clock pulses on the sck line to interchange data. data is always shifted from master to slave on the master out ? slave in, mosi, line, and from slave to master on the master in ? slave out, miso, line. after each data packet, the master will synchronize the slave by pulling high the slave select, ss, line. when configured as a master, the spi interface has no automatic control of the ss line. this must be handled by user software before communication can start. when this is done, writing a byte to the spi data register starts the spi clock generator, and the hardware shifts the eight bits into the slave. after shifting one byte, the spi clock generator stops, setting the end of transmission flag (spif). if the spi interrupt enable bit (spie) in the spcr register is set, an spi2x spi2x clkio ss divider /2/4/8/16/32/64/128
127 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 interrupt is requested. the master may continue to shift the next byte by writing it into spdr, or signal the end of packet by pulling high the slave select, ss line. the last incoming byte will be kept in the buffer register for later use. when configured as a slave, the spi interface will remain sleeping with miso tri-stated as long as the ss pin is driven high. in this state, software may update the contents of the spi data register, spdr, but the data will not be shifted out by incoming clock pulses on the sck pin until the ss pin is driven low. as one byte has been completely shifted, the end of transmission flag, spif is set. if the spi interrupt enable bit, spie, in the spcr register is set, an interrupt is requested. the slave may continue to place new data to be sent into spdr before reading the incoming data. the last incoming byte will be kept in the buffer register for later use. figure 16-2. spi master-slave interconnection the system is single buffered in the transmit direction and double buffered in the receive direction. this means that bytes to be transmitted cannot be written to the spi data register before the entire shift cycle is completed. when receiving data, however, a received character must be read from the spi data register before the next character has been completely shifted in. otherwise, the first byte is lost. in spi slave mode, the control logic will sample the incoming signal of the sck pin. to ensure correct sampling of the clock signal, the minimum low and high periods should be: low periods: longer than 2 cpu clock cycles. high periods: longer than 2 cpu clock cycles. when the spi is enabled, the data direction of the mosi, miso, sck, and ss pins is overridden according to table 16- 1 . for more details on automatic port overrides, refer to ?alternate port functions? on page 47 . table 16-1. spi pin overrides note: see ?alternate functions of port b? on page 53 for a detailed description of how to define the direction of the user defined spi pins. pin direction, master spi direction, slave spi mosi user defined input miso input user defined sck user defined input ss user defined input shift enable
128 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 the following code examples show how to initialize the spi as a master and how to perform a simple transmission. ddr_spi in the examples must be replaced by the actual data direction register controlling the spi pins. dd_mosi, dd_miso and dd_sck must be replaced by the actual data direction bits for these pins. e.g. if mosi is placed on pin pb5, replace dd_mosi with ddb5 and ddr_spi with ddrb. assembly code example spi_masterinit: ; set mosi and sck output, all others input ldi r17,(1< 129 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 note: see ?code examples? on page 7. the following code examples show how to initialize the spi as a slave and how to perform a simple reception. c code example void spi_masterinit( void ) { /* set mosi and sck output, all others input */ ddr_spi = (1< 130 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 note: see ?code examples? on page 7. 16.3 ss pin functionality 16.3.1 slave mode when the spi is configured as a slave, the slave select ( ss) pin is always input. when ss is held low, the spi is activated, and miso becomes an output if configured so by the user. all other pins are inputs. when ss is driven high, all pins are inputs, and the spi is passive, which means that it will not receive incoming data. note that the spi logic will be reset once the ss pin is driven high. the ss pin is useful for packet/byte synchronization to keep the slave bit counter synchronous with the master clock generator. when the ss pin is driven high, the spi slave will immediately reset the send and receive logic, and drop any partially received data in the shift register. 16.3.2 master mode when the spi is configured as a master (mstr in spcr is set), the user can determine the direction of the ss pin. if ss is configured as an output, the pin is a general output pin which does not affect the spi system. typically, the pin will be driving the ss pin of the spi slave. if ss is configured as an input, it must be held high to ensure master spi operation. if the ss pin is driven low by peripheral circuitry when the spi is configured as a master with the ss pin defined as an input, the spi system interprets this as another master selecting the spi as a slave and starting to send data to it. to avoid bus contention, the spi system takes the following actions: 1. the mstr bit in spcr is cleared and the spi system becomes a slave. as a result of the spi becoming a slave, the mosi and sck pins become inputs. 2. the spif flag in spsr is set, and if the spi interrupt is enabled, and the i-bit in sreg is set, the interrupt routine will be executed. thus, when interrupt-driven spi transmission is used in master mode, and there exists a possibility that ss is driven low, the interrupt should always check that the mstr bit is still set. if the mstr bit has been cleared by a slave select, it must be set by the user to re-enable spi master mode. c code example void spi_slaveinit( void ) { /* set miso output, all others input */ ddr_spi = (1< 131 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 16.4 data modes there are four combinations of sck phase and polarity with respect to serial data, which are determined by control bits cpha and cpol. the spi data transfer formats are shown in figure 16-3 on page 131 and figure 16-4 on page 131 . figure 16-3. spi transfer format with cpha = 0 figure 16-4. spi transfer format with cpha = 1 data bits are shifted out and latched in on opposite edges of the sck signal, ensuring sufficient time for data signals to stabilize. this is shown in table 16-2 , which is a summary of table 16-3 and table 16-4 . bit 1 bit 6 lsb msb sck (cpol = 0) mode 0 sample i mosi/miso change 0 mosi pin change 0 miso pin sck (cpol = 1) mode 2 ss msb lsb bit 6 bit 1 bit 5 bit 2 bit 4 bit 3 bit 3 bit 4 bit 2 bit 5 msb first (dord = 0) lsb first (dord = 1) sck (cpol = 0) mode 1 sample i mosi/miso change 0 mosi pin change 0 miso pin sck (cpol = 1) mode 3 ss msb lsb bit 6 bit 1 bit 5 bit 2 bit 4 bit 3 bit 3 bit 4 bit 2 bit 5 bit 1 bit 6 lsb msb msb first (dord = 0) lsb first (dord = 1)
132 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 table 16-2. spi modes 16.5 register description 16.5.1 spcr ? spi control register z bit 7 ? spie: spi interrupt enable this bit causes the spi interrupt to be executed if spif bit in the spsr register is set and the if the global interrupt enable bit in sreg is set. z bit 6 ? spe: spi enable when the spe bit is written to one, the spi is enabled. this bit must be set to enable any spi operations. z bit 5 ? dord: data order when the dord bit is written to one, the lsb of the data word is transmitted first. when the dord bit is written to zero, the msb of the data word is transmitted first. z bit 4 ? mstr: master/slave select this bit selects master spi mode when written to one, and slave spi mode when written logic zero. if ss is configured as an input and is driven low while mstr is set, mstr will be cleared, and spif in spsr will become set. the user will then have to set mstr to re-enable spi master mode. z bit 3 ? cpol: clock polarity when this bit is written to one, sck is high when idle. when cpol is written to zero, sck is low when idle. refer to figure 16-3 and figure 16-4 for an example. the cpol functionality is summarized below: table 16-3. cpol functionality z bit 2 ? cpha: clock phase the settings of the clock phase bit (cpha) determine if data is sampled on the leading (first) or trailing (last) edge of sck. refer to figure 16-3 and figure 16-4 for an example. the cpol functionality is summarized below: spi mode conditions leading edge trailing edge 0 cpol=0, cpha=0 sample (rising) setup (falling) 1 cpol=0, cpha=1 setup (rising) sample (falling) 2 cpol=1, cpha=0 sample (falling) setup (rising) 3 cpol=1, cpha=1 setup (falling) sample (rising) bit 76543210 0x30 spie spe dord mstr cpol cpha spr1 spr0 spcr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 00000000 cpol leading edge trailing edge 0 rising falling 1 falling rising
133 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 table 16-4. cpha functionality z bits 1:0 ? spr[1:0]: spi clock rate select 1 and 0 these two bits control the sck rate of the device configured as a master. spr1 and spr0 have no effect on the slave. the relationship between sck and the i/o clock frequency f clk_i/o is shown in the following table: table 16-5. relationship between sck and the i/o clock frequency 16.5.2 spsr ? spi status register z bit 7 ? spif: spi interrupt flag when a serial transfer is complete, the spif flag is set. an interrupt is generated if spie in spcr is set and global interrupts are enabled. if ss is an input and is driven low when the spi is in master mode, this will also set the spif flag. spif is cleared by hardware when executing the corresponding interrupt handling vector. alternatively, the spif bit is cleared by first reading the spi status register with spif set, then accessing the spi data register (spdr). z bit 6 ? wcol: write collision flag the wcol bit is set if the spi data register (spdr) is written during a data transfer. the wcol bit (and the spif bit) are cleared by first reading the spi status register with wcol set, and then accessing the spi data register. z bits 5:1 ? res: reserved bits these bits are reserved and will always read as zero. z bit 0 ? spi2x: double spi speed bit when this bit is written logic one the spi speed (sck frequency) will be doubled when the spi is in master mode (see table 16-5 ). this means that the minimum sck period will be two i/o clock periods. when the spi is configured as slave, the spi is only guaranteed to work at f clk_i/o /4 or lower. cpha leading edge trailing edge 0 sample setup 1 setup sample spi2x spr1 spr0 sck frequency 0 0 0 f clk_i/o / 4 0 0 1 f clk_i/o / 16 0 1 0 f clk_i/o / 64 0 1 1 f clk_i/o / 128 1 0 0 f clk_i/o / 2 1 0 1 f clk_i/o / 8 1 1 0 f clk_i/o / 32 1 1 1 f clk_i/o / 64 bit 76543210 0x2f spif wcol ????? spi2x spsr read/write r/w r/w rrrrrr/w initial value 00000000
134 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 16.5.3 spdr ? spi data register the spi data register is a read/write register used for data transfer between the register file and the spi shift register. writing to the register initiates data transmission. reading the register causes the shift register receive buffer to be read. bit 76543210 0x2e msb lsb spdr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value x x x x x x x x undefined
135 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 17. twi ? two wire slave interface 17.1 features z phillips i 2 c compatible z smbus compatible (with reservations) z 100 khz and 400 khz support at low system clock frequencies z slew-rate limited output drivers z input filter provides noise suppression z 7-bit, and general call address recognition in hardware z address mask register for address masking or dual address match z 10-bit addressing supported z optional software address recognition provides unlimited number of slave addresses z slave can operate in all sleep modes, including power down z slave arbitration allows support for smbus address resolve protocol (arp) 17.2 overview the two wire interface (twi) is a bi-directional, bus communication interface, which uses only two wires. the twi is i 2 c compatible and, with reservations, smbus compatible (see ?compatibility with smbus? on page 141 ). a device connected to the bus must act as a master or slave.the master initiates a data transaction by addressing a slave on the bus, and telling whether it wants to transmit or receive data. one bus can have several masters, and an arbitration process handles priority if two or more masters try to transmit at the same time. the twi module in attiny20 implements slave functionality, only. lost arbitration, errors, collisions and clock holds on the bus are detected in hardware and indicated in separate status flags. both 7-bit and general address call recognition is implemented in hardware. 10-bit addressing is also supported. a dedicated address mask register can act as a second address match register or as a mask register for the slave address to match on a range of addresses. the slave logic continues to operate in all sleep modes, including power down. this enables the slave to wake up from sleep on twi address match. it is possible to disable the address matching and let this be handled in software instead. this allows the slave to detect and respond to several addresses. smart mode can be enabled to auto trigger operations and reduce software complexity. the twi module includes bus state logic that collects information to detect start and stop conditions, bus collision and bus errors. the bus state logic continues to operate in all sleep modes including power down. 17.3 general twi bus concepts the two-wire interface (twi) provides a simple two-wire bi-directional bus consisting of a serial clock line (scl) and a serial data line (sda). the two lines are open collector lines (wired-and), and pull-up resistors (rp) are the only external components needed to drive the bus. the pull-up resistors will provide a high level on the lines when none of the connected devices are driving the bus. a constant current source can be used as an alternative to the pull-up resistors. the twi bus is a simple and efficient method of interconnecting multiple devices on a serial bus. a device connected to the bus can be a master or slave, where the master controls the bus and all communication. figure 17-1 illustrates the twi bus topology.
136 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 17-1. twi bus topology a unique address is assigned to all slave devices connected to the bus, and the master will use this to address a slave and initiate a data transaction. 7-bit or 10-bit addressing can be used. several masters can be connected to the same bus, and this is called a multi-master environment. an arbitration mechanism is provided for resolving bus ownership between masters since only one master device may own the bus at any given time. a device can contain both master and slave logic, and can emulate multiple slave devices by responding to more than one address. a master indicates the start of transaction by issuing a start condition (s) on the bus. an address packet with a slave address (address) and an indication whether the master wishes to read or write data (r/ w), is then sent. after all data packets (data) are transferred, the master issues a stop condition (p) on the bus to end the transaction. the receiver must acknowledge (a) or not-acknowledge ( a) each byte received. figure 17-2 shows a twi transaction. figure 17-2. basic twi transaction diagram topology
137 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 the master provides the clock signal for the transaction, but a device connected to the bus is allowed to stretch the low level period of the clock to decrease the clock speed. 17.3.1 electrical characteristics the twi follows the electrical specifications and timing of i 2 c and smbus. see ?compatibility with smbus? on page 141 . 17.3.2 start and stop conditions two unique bus conditions are used for marking the beginning (start) and end (stop) of a transaction. the master issues a start condition(s) by indicating a high to low transition on the sda line while the scl line is kept high. the master completes the transaction by issuing a stop condition (p), indicated by a low to high transition on the sda line while scl line is kept high. figure 17-3. start and stop conditions multiple start conditions can be issued during a single transaction. a start condition not directly following a stop condition, are named a repeated start condition (sr). 17.3.3 bit transfer as illustrated by figure 17-4 a bit transferred on the sda line must be stable for the entire high period of the scl line. consequently the sda value can only be changed during the low period of the clock. this is ensured in hardware by the twi module. figure 17-4. data validity combining bit transfers results in the formation of address and data packets. these packets consist of 8 data bits (one byte) with the most significant bit transferred first, plus a single bit not-acknowledge (nack) or acknowledge (ack) response. the addressed device signals ack by pulling the scl line low, and nack by leaving the line scl high during the ninth clock cycle.
138 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 17.3.4 address packet after the start condition, a 7-bit address followed by a read/write (r/ w) bit is sent. this is always transmitted by the master. a slave recognizing its address will ack the address by pulling the data line low the next scl cycle, while all other slaves should keep the twi lines released, and wait for the next start and address. the 7-bit address, the r/ w bit and the acknowledge bit combined is the address packet. only one address packet for each start condition is given, also when 10-bit addressing is used. the r/ w specifies the direction of the transaction. if the r/ w bit is low, it indicates a master write transaction, and the master will transmit its data after the slave has acknowledged its address. opposite, for a master read operation the slave will start to transmit data after acknowledging its address. 17.3.5 data packet data packets succeed an address packet or another data packet. all data packets are nine bits long, consisting of one data byte and an acknowledge bit. the direction bit in the previous address packet determines the direction in which the data is transferred. 17.3.6 transaction a transaction is the complete transfer from a start to a stop condition, including any repeated start conditions in between. the twi standard defines three fundamental transaction modes: master write, master read, and combined transaction. figure 17-5 illustrates the master write transaction. the master initiates the transaction by issuing a start condition (s) followed by an address packet with direction bit set to zero (address+ w). figure 17-5. master write transaction given that the slave acknowledges the address, the master can start transmitting data (data) and the slave will ack or nack (a/ a) each byte. if no data packets are to be transmitted, the master terminates the transaction by issuing a stop condition (p) directly after the address packet. there are no limitations to the number of data packets that can be transferred. if the slave signal a nack to the data, the master must assume that the slave cannot receive any more data and terminate the transaction. figure 17-6 illustrates the master read transaction. the master initiates the transaction by issuing a start condition followed by an address packet with direction bit set to one (adress+r). the addressed slave must acknowledge the address for the master to be allowed to continue the transaction. figure 17-6. master read transaction
139 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 given that the slave acknowledges the address, the master can start receiving data from the slave. there are no limitations to the number of data packets that can be transferred. the slave transmits the data while the master signals ack or nack after each data byte. the master terminates the transfer with a nack before issuing a stop condition. figure 17-7 illustrates a combined transaction. a combined transaction consists of several read and write transactions separated by a repeated start conditions (sr). figure 17-7. combined transaction 17.3.7 clock and clock stretching all devices connected to the bus are allowed to stretch the low period of the clock to slow down the overall clock frequency or to insert wait states while processing data. a device that needs to stretch the clock can do this by holding/forcing the scl line low after it detects a low level on the line. three types of clock stretching can be defined as shown in figure 17-8 . figure 17-8. clock stretching if the device is in a sleep mode and a start condition is detected the clock is stretched during the wake-up period for the device. a slave device can slow down the bus frequency by stretching the clock periodically on a bit level. this allows the slave to run at a lower system clock frequency. however, the overall performance of the bus will be reduced accordingly. both the master and slave device can randomly stretch the clock on a byte level basis before and after the ack/nack bit. this provides time to process incoming or prepare outgoing data, or performing other time critical tasks. in the case where the slave is stretching the clock the master will be forced into a wait-state until the slave is ready and vice versa. 17.3.8 arbitration a master can only start a bus transaction if it has detected that the bus is idle. as the twi bus is a multi master bus, it is possible that two devices initiate a transaction at the same time. this results in multiple masters owning the bus simultaneously. this is solved using an arbitration scheme where the master loses control of the bus if it is not able to transmit a high level on the sda line. the masters who lose arbitration must then wait until the bus becomes idle (i.e. wait for a stop condition) before attempting to reacquire bus ownership. slave devices are not involved in the arbitration procedure.
140 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 17-9. twi arbitration figure 17-9 shows an example where two twi masters are contending for bus ownership. both devices are able to issue a start condition, but device1 loses arbitration when attempting to transmit a high level (bit 5) while device2 is transmitting a low level. arbitration between a repeated start condition and a data bit, a stop condition and a data bit, or a repeated start condition and stop condition are not allowed and will require special handling by software. 17.3.9 synchronization a clock synchronization algorithm is necessary for solving situations where more than one master is trying to control the scl line at the same time. the algorithm is based on the same principles used for clock stretching previously described. figure 17-10 shows an example where two masters are competing for the control over the bus clock. the scl line is the wired-and result of the two masters clock outputs. figure 17-10.clock synchronization a high to low transition on the scl line will force the line low for all masters on the bus and they start timing their low clock period. the timing length of the low clock period can vary between the masters. when a master (device1 in this
141 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 case) has completed its low period it releases the scl line. however, the scl line will not go high before all masters have released it. consequently the scl line will be held low by the device with the longest low period (device2). devices with shorter low periods must insert a wait-state until the clock is released. all masters start their high period when the scl line is released by all devices and has become high. the device which first completes its high period (device1) forces the clock line low and the procedure are then repeated. the result of this is that the device with the shortest clock period determines the high period while the low period of the clock is determined by the longest clock period. 17.3.10 compatibility with smbus as with any other i 2 c-compliant interface there are known compatibility issues the designer should be aware of before connecting a twi device to smbus devices. for use in smbus environments, the following should be noted: z all i/o pins of an avr, including those of the two-wire interface, have protection diodes to both supply voltage and ground. see figure 10-1 on page 42 . this is in contradiction to the requirements of the smbus specifications. as a result, supply voltage mustn?t be removed from the avr or the protection diodes will pull the bus lines down. power down and sleep modes is not a problem, provided supply voltages remain. z the data hold time of the twi is lower than specified for smbus. the twshe bit of twscra can be used to increase the hold time. see ?twscra ? twi slave control register a? on page 143 . z smbus has a low speed limit, while i 2 c hasn?t. as a master in an smbus environment, the avr must make sure bus speed does not drop below specifications, since lower bus speeds trigger timeouts in smbus slaves. if the avr is configured a slave there is a possibility of a bus lockup, since the twi module doesn't identify timeouts. 17.4 twi slave operation the twi slave is byte-oriented with optional interrupts after each byte. there are separate interrupt flags for data interrupt and address/stop interrupt. interrupt flags can be set to trigger the twi interrupt, or be used for polled operation. there are dedicated status flags for indicating ack/nack received, clock hold, collision, bus error and read/write direction. when an interrupt flag is set, the scl line is forced low. this will give the slave time to respond or handle any data, and will in most cases require software interaction. figure 17-11 . shows the twi slave operation. the diamond shapes symbols (sw) indicate where software interaction is required. figure 17-11.twi slave operation the number of interrupts generated is kept at a minimum by automatic handling of most conditions. quick command can be enabled to auto trigger operations and reduce software complexity. promiscuous mode can be enabled to allow the slave to respond to all received addresses.
142 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 17.4.1 receiving address packets when the twi slave is properly configured, it will wait for a start condition to be detected. when this happens, the successive address byte will be received and checked by the address match logic, and the slave will ack the correct address. if the received address is not a match, the slave will not acknowledge the address and wait for a new start condition. the slave address/stop interrupt flag is set when a start condition succeeded by a valid address packet is detected. a general call address will also set the interrupt flag. a start condition immediately followed by a stop condition, is an illegal operation and the bus error flag is set. the r/w direction flag reflects the direction bit received with the address. this can be read by software to determine the type of operation currently in progress. depending on the r/w direction bit and bus condition one of four distinct cases (1 to 4) arises following the address packet. the different cases must be handled in software. 17.4.1.1 case 1: address packet accepted - direction bit set if the r/ w direction flag is set, this indicates a master read operation. the scl line is forced low, stretching the bus clock. if ack is sent by the slave, the slave hardware will set the data interrupt flag indicating data is needed for transmit. if nack is sent by the slave, the slave will wait for a new start condition and address match. 17.4.1.2 case 2: address packet accepted - direction bit cleared if the r/ w direction flag is cleared this indicates a master write operation. the scl line is forced low, stretching the bus clock. if ack is sent by the slave, the slave will wait for data to be received. data, repeated start or stop can be received after this. if nack is indicated the slave will wait for a new start condition and address match. 17.4.1.3 case 3: collision if the slave is not able to send a high level or nack, the collision flag is set and it will disable the data and acknowledge output from the slave logic. the clock hold is released. a start or repeated start condition will be accepted. 17.4.1.4 case 4: stop condition received. operation is the same as case 1 or 2 above with one exception. when the stop condition is received, the slave address/stop flag will be set indicating that a stop condition and not an address match occurred. 17.4.2 receiving data packets the slave will know when an address packet with r/ w direction bit cleared has been successfully received. after acknowledging this, the slave must be ready to receive data. when a data packet is received the data interrupt flag is set, and the slave must indicate ack or nack. after indicating a nack, the slave must expect a stop or repeated start condition. 17.4.3 transmitting data packets the slave will know when an address packet, with r/ w direction bit set, has been successfully received. it can then start sending data by writing to the slave data register. when a data packet transmission is completed, the data interrupt flag is set. if the master indicates nack, the slave must stop transmitting data, and expect a stop or repeated start condition.
143 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 17.5 register description 17.5.1 twscra ? twi slave control register a z bit 7 ? twshe: twi sda hold time enable when this bit is set each negative transition of scl triggers an additional internal delay, before the device is allowed to change the sda line. the added delay is approximately 50ns in length. this may be useful in smbus systems. z bit 6 ? res: reserved bit this bit is reserved and will always read as zero. z bit 5 ? twdie: twi data interrupt enable when this bit is set and interrupts are enabled, a twi interrupt will be generated when the data interrupt flag (twdif) in twssra is set. z bit 4 ? twasie: twi address/stop interrupt enable when this bit is set and interrupts are enabled, a twi interrupt will be generated when the address/stop interrupt flag (twasif) in twssra is set. z bit 3 ? twen: two-wire interface enable when this bit is set the slave two-wire interface is enabled. z bit 2 ? twsie: twi stop interrupt enable setting the stop interrupt enable (twsie) bit will set the twasif in the twssra register when a stop condition is detected. z bit 1 ? twpme: twi promiscuous mode enable when this bit is set the address match logic of the slave twi responds to all received addresses. when this bit is cleared the address match logic uses the twsa register to determine which address to recognize as its own. z bit 0 ? twsme: twi smart mode enable when this bit is set the twi slave enters smart mode, where the acknowledge action is sent immediately after the twi data register (twsd) has been read. acknowledge action is defined by the twaa bit in twscrb. when this bit is cleared the acknowledge action is sent after twcmdn bits in twscrb are written to 1x. bit 76543210 0x2d twshe ? twdie twasie twen twsie twpme twsme twscra read/write r/w r r/w r/w r/w r/w r/w r/w initial value 00000000
144 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 17.5.2 twscrb ? twi slave control register b z bits 7:3 ? res: reserved bits these bits are reserved and will always read as zero. z bit 2 ? twaa: twi acknowledge action this bit defines the slave's acknowledge behavior after an address or data byte has been received from the master. depending on the twsme bit in twscra the acknowledge action is executed either when a valid command has been written to twcmdn bits, or when the data register has been read. acknowledge action is also executed if clearing twaif flag after address match or twdif flag during master transmit. see table 17-1 for details. table 17-1. acknowledge action of twi slave z bits 1:0 ? twcmd[1:0]: twi command writing these bits triggers the slave operation as defined by table 17-2 . the type of operation depends on the twi slave interrupt flags, twdif and twasif. the acknowledge action is only executed when the slave receives data bytes or address byte from the master. table 17-2. twi slave command writing the twcmd bits will automatically release the scl line and clear the twch and slave interrupt flags. bit 76543210 0x2c ?????twaa twcmd1 twcmd0 twscrb read/write rrrrrr/www initial value 00000000 twaa action twsme when 0 send ack 0 when twcmdn bits are written to 10 or 11 1 when twsd is read 1 send nack 0 when twcmdn bits are written to 10 or 11 1 when twsd is read twcmd[1:0] twdir operation use 00 x no action none 01 x reserved 10 0 execute acknowledge action, then wait for any start (s/sr) condition to complete transaction 1 wait for any start (s/sr) condition 11 0 execute acknowledge action, then receive next byte in response to an address byte (twasif is set) 1 execute acknowledge action, then set twdif 0 execute acknowledge action, then wait for next byte in response to a data byte (twdif is set) 1 no action
145 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 twaa and twcmdn bits can be written at the same time. acknowledge action will then be executed before the command is triggered. the twcmdn bits are strobed and always read zero. 17.5.3 twssra ? twi slave status register a z bit 7 ? twdif: twi data interrupt flag this flag is set when a data byte has been successfully received, i.e. no bus errors or collisions have occurred during the operation. when this flag is set the slave forces the scl line low, stretching the twi clock period. the scl line is released by clearing the interrupt flags. writing a one to this bit will clear the flag. this flag is also automatically cleared when writing a valid command to the twcmdn bits in twscrb. z bit 6 ? twasif: twi address/stop interrupt flag this flag is set when the slave detects that a valid address has been received, or when a transmit collision has been detected. when this flag is set the slave forces the scl line low, stretching the twi clock period. the scl line is released by clearing the interrupt flags. if twasie in twscra is set, a stop condition on the bus will also set twasif. stop condition will set the flag only if system clock is faster than the minimum bus free time between stop and start. writing a one to this bit will clear the flag. this flag is also automatically cleared when writing a valid command to the twcmdn bits in twscrb. z bit 5 ? twch: twi clock hold this bit is set when the slave is holding the scl line low. this bit is read-only, and set when twdif or twasif is set. the bit can be cleared indirectly by clearing the interrupt flags and releasing the scl line. z bit 4 ? twra: twi receive acknowledge this bit contains the most recently received acknowledge bit from the master. this bit is read-only. when zero, the most recent acknowledge bit from the maser was ack and, when one, the most recent acknowledge bit was nack. z bit 3 ? twc: twi collision this bit is set when the slave was not able to transfer a high data bit or a nack bit. when a collision is detected, the slave will commence its normal operation, and disable data and acknowledge output. no low values are shifted out onto the sda line. this bit is cleared by writing a one to it. the bit is also cleared automatically when a start or repeated start condition is detected. z bit 2 ? twbe: twi bus error this bit is set when an illegal bus condition has occurred during a transfer. an illegal bus condition occurs if a repeated start or stop condition is detected, and the number of bits from the previous start condition is not a multiple of nine. bit 76543210 0x2b twdif twasif twch twra twc twbe twdir twas twssra read/write r/w r/w r r r/w r/w r/w r/w initial value 00000000
146 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 this bit is cleared by writing a one to it. for bus errors to be detected, the system clock frequency must be at least four times the scl frequency. z bit 1 ? twdir: twi read/write direction this bit indicates the direction bit from the last address packet received from a master. when this bit is one, a master read operation is in progress. when the bit is zero a master write operation is in progress. z bit 0 ? twas: twi address or stop this bit indicates why the twasif bit was last set. if zero, a stop condition caused twasif to be set. if one, address detection caused twasif to be set. 17.5.4 twsa ? twi slave address register the slave address register contains the twi slave address used by the slave address match logic to determine if a master has addressed the slave. when using 7-bit or 10-bit address recognition mode, the high seven bits of the address register (twsa[7:1]) represent the slave address. the least significant bit (twsa0) is used for general call address recognition. setting twsa0 enables general call address recognition logic. when using 10-bit addressing the address match logic only support hardware address recognition of the first byte of a 10-bit address. if twsa[7:1] is set to "0b11110nn", 'nn' will represent bits 9 and 8 of the slave address. the next byte received is then bits 7 to 0 in the 10-bit address, but this must be handled by software. when the address match logic detects that a valid address byte has been received, the twasif is set and the twdir flag is updated. if twpme in twscra is set, the address match logic responds to all addresses transmitted on the twi bus. twsa is not used in this mode. 17.5.5 twsd ? twi slave data register the data register is used when transmitting and received data. during transfer, data is shifted from/to the twsd register and to/from the bus. therefore, the data register cannot be accessed during byte transfers. this is protected in hardware. the data register can only be accessed when the scl line is held low by the slave, i.e. when twch is set. when a master reads data from a slave, the data to be sent must be written to the twsd register. the byte transfer is started when the master starts to clock the data byte from the slave. it is followed by the slave receiving the acknowledge bit from the master. the twdif and the twch bits are then set. when a master writes data to a slave, the twdif and the twch flags are set when one byte has been received in the data register. if smart mode is enabled, reading the data register will trigger the bus operation, as set by the twaa bit in twscrb. accessing twsd in smart mode will clear the slave data interrupt flag and the twch bit. bit 76543210 0x2a twsa[7:0] twsa read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 00000000 bit 76543210 0x28 twsd[7:0] twsd read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 00000000
147 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 17.5.6 twsam ? twi slave address mask register z bits 7:1 ? twsam[7:1]: twi address mask these bits can act as a second address match register, or an address mask register, depending on the twae setting. if twae is set to zero, twsam can be loaded with a 7-bit slave address mask. each bit in twsam can mask (disable) the corresponding address bit in the twsa register. if the mask bit is one the address match between the incoming address bit and the corresponding bit in twsa is ignored. in other words, masked bits will always match. if twae is set to one, twsam can be loaded with a second slave address in addition to the twsa register. in this mode, the slave will match on 2 unique addresses, one in twsa and the other in twsam. z bit 0 ? twae: twi address enable by default, this bit is zero and the twsam bits acts as an address mask to the twsa register. if this bit is set to one, the slave address match logic responds to the two unique addresses in twsa and twsam. bit 76543210 0x29 twsam[7:1] twae twsam read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 00000000
148 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 18. programming interface 18.1 features z physical layer: z synchronous data transfer z bi-directional, half-duplex receiver and transmitter z fixed frame format with one start bit, 8 data bits, one parity bit and 2 stop bits z parity error detection, frame error detection and break character detection z parity generation and collision detection z automatic guard time insertion between data reception and transmission z access layer: z communication based on messages z automatic exception handling mechanism z compact instruction set z nvm programming access control z tiny programming interface control and status space access control z data space access control 18.2 overview the tiny programming interface (tpi) supports external programming of all non-volatile memories (nvm). memory programming is done via the nvm controller, by executing nvm controller commands as described in ?memory programming? on page 159 . the tiny programming interface (tpi) provides access to the programming facilities. the interface consists of two layers: the access layer and the physical layer. the layers are illustrated in figure 18-1 . figure 18-1. the tiny programming interface and related internal interfaces programming is done via the physical interface. this is a 3-pin interface, which uses the reset pin as enable, the tpiclk pin as the clock input, and the tpidata pin as data input and output. nvm can be programmed at 5v, only. 18.3 physical layer of tiny programming interface the tpi physical layer handles the basic low-level serial communication. the tpi physical layer uses a bi-directional, half-duplex serial receiver and transmitter. the physical layer includes serial-to-parallel and parallel-to-serial data conversion, start-of-frame detection, frame error detection, parity error detection, parity generation and collision detection. access layer physical layer nvm controller non-volatile memories tpiclk reset t pidata tiny programming interface (tpi) data bus
149 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 the tpi is accessed via three pins, as follows: reset: tiny programming interface enable input tpiclk: tiny programming interface clock input tpidata: tiny programming interface data input/output in addition, the v cc and gnd pins must be connected between the external programmer and the device. 18.3.1 enabling the following sequence enables the tiny programming interface: z apply 5v between v cc and gnd z depending on the method of reset to be used: z either: wait t tout (see table 20-4 on page 170 ) and then set the reset pin low. this will reset the device and enable the tpi physical layer. the reset pin must then be kept low for the entire programming session z or: if the rstdisbl configuration bit has been programmed, apply 12v to the reset pin. the reset pin must be kept at 12v for the entire programming session z wait t rst (see table 20-4 on page 170 ) z keep the tpidata pin high for 16 tpiclk cycles see figure 18-2 for guidance. figure 18-2. sequence for enabling the tiny programming interface 18.3.2 disabling provided that the nvm enable bit has been cleared, the tpi is automatically disabled if the reset pin is released to inactive high state or, alternatively, if v hv is no longer applied to the reset pin. if the nvm enable bit is not cleared a power down is required to exit tpi programming mode. see nvmen bit in ?tpisr ? tiny programming interface status register? on page 158 . 18.3.3 frame format the tpi physical layer supports a fixed frame format. a frame consists of one character, eight bits in length, and one start bit, a parity bit and two stop bits. data is transferred with the least significant bit first. figure 18-3. serial frame format. reset t rst tpidata tpiclk 16 x tpiclk cycles tpidata tpiclk sp1 st sp2 idle/st idle p d1 d0 d7
150 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 symbols used in figure 18-3 : st: start bit (always low) d0-d7: data bits (least significant bit sent first) p: parity bit (using even parity) sp1: stop bit 1 (always high) sp2: stop bit 2 (always high) 18.3.4 parity bit calculation the parity bit is always calculated using even parity. the value of the bit is calculated by doing an exclusive-or of all the data bits, as follows: p = d0 ? d1 ? d2 ? d3 ? d4 ? d5 ? d6 ? d7 ? 0 where: p: parity bit using even parity d0-d7: data bits of the character 18.3.5 supported characters the break character is equal to a 12 bit long low level. it can be extended beyond a bit-length of 12. the idle character is equal to a 12 bit long high level. it can be extended beyond a bit-length of 12. figure 18-4. supported characters. 18.3.6 operation the tpi physical layer operates synchronously on the tpiclk provided by the external programmer. the dependency between the clock edges and data sampling or data change is shown in figure 18-5 . data is changed at falling edges and sampled at rising edges. idle/st idle break character data character sp1 st sp2 idle/st idle p d1 d0 d7 tpidata idle/st idle idle character tpidata tpidata
151 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 18-5. data changing and data sampling. the tpi physical layer supports two modes of operation: transmit and receive. by default, the layer is in receive mode, waiting for a start bit. the mode of operation is controlled by the access layer. 18.3.7 serial data reception when the tpi physical layer is in receive mode, data reception is started as soon as a start bit has been detected. each bit that follows the start bit will be sampled at the rising edge of the tpiclk and shifted into the shift register until the second stop bit has been received. when the complete frame is present in the shift register the received data will be available for the tpi access layer. there are three possible exceptions in the receive mode: frame error, parity error and break detection. all these exceptions are signalized to the tpi access layer, which then enters the error state and puts the tpi physical layer into receive mode, waiting for a break character. z frame error exception. the frame error exception indicates the state of the stop bit. the frame error exception is set if the stop bit was read as zero. z parity error exception. the parity of the data bits is calculated during the frame reception. after the frame is received completely, the result is compared with the parity bit of the frame. if the comparison fails the parity error exception is signalized. z break detection exception. the break detection exception is given when a complete frame of all zeros has been received. 18.3.8 serial data transmission when the tpi physical layer is ready to send a new frame it initiates data transmission by loading the shift register with the data to be transmitted. when the shift register has been loaded with new data, the transmitter shifts one complete frame out on the tpidata line at the transfer rate given by tpiclk. if a collision is detected during transmission, the output driver is disabled. the tpi access layer enters the error state and the tpi physical layer is put into receive mode, waiting for a break character. 18.3.9 collision detection exception the tpi physical layer uses one bi-directional data line for both data reception and transmission. a possible drive contention may occur, if the external programmer and the tpi physical layer drive the tpidata line simultaneously. in order to reduce the effect of the drive contention, a collision detection mechanism is supported. the collision detection is based on the way the tpi physical layer drives the tpidata line. the tpidata line is driven by a tri-state, push-pull driver with internal pull-up. the output driver is always enabled when a logical zero is sent. when sending successive logical ones, the output is only driven actively during the first clock cycle. after this, the output driver is automatically tri-stated and the tpidata line is kept high by the internal pull-up. the output is re-enabled, when the next logical zero is sent. tpidata tpiclk sample setup
152 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 the collision detection is enabled in transmit mode, when the output driver has been disabled. the data line should now be kept high by the internal pull-up and it is monitored to see, if it is driven low by the external programmer. if the output is read low, a collision has been detected. there are some potential pit-falls related to the way collision detection is performed. for example, collisions cannot be detected when the tpi physical layer transmits a bit-stream of successive logical zeros, or bit-stream of alternating logical ones and zeros. this is because the output driver is active all the time, preventing polling of the tpidata line. however, within a single frame the two stop bits should always be transmitted as logical ones, enabling collision detection at least once per frame (as long as the frame format is not violated regarding the stop bits). the tpi physical layer will cease transmission when it detects a collision on the tpidata line. the collision is signalized to the tpi access layer, which immediately changes the physical layer to receive mode and goes to the error state. the tpi access layer can be recovered from the error state only by sending a break character. 18.3.10 direction change in order to ensure correct timing of the half-duplex operation, a simple guard time mechanism has been added to the physical layer. when the tpi physical layer changes from receive to transmit mode, a configurable number of additional idle bits are inserted before the start bit is transmitted. the minimum transition time between receive and transmit mode is two idle bits. the total idle time is the specified guard time plus two idle bits. the guard time is configured by dedicated bits in the tpipcr register. the default guard time value after the physical layer is initialized is 128 bits. the external programmer looses control of the tpidata line when the tpi target changes from receive mode to transmit. the guard time feature relaxes this critical phase of the communication. when the external programmer changes from receive mode to transmit, a minimum of one idle bit should be inserted before the start bit is transmitted. 18.4 access layer of tiny programming interface the tpi access layer is responsible for handling the communication with the external programmer. the communication is based on a message format, where each message comprises an instruction followed by one or more byte-sized operands. the instruction is always sent by the external programmer but operands are sent either by the external programmer or by the tpi access layer, depending on the type of instruction issued. the tpi access layer controls the character transfer direction on the tpi physical layer. it also handles the recovery from the error state after exception. the control and status space (css) of the tiny programming interface is allocated for control and status registers in the tpi access layer. the css consist of registers directly involved in the operation of the tpi itself. these register are accessible using the sldcs and sstcs instructions. the access layer can also access the data space, either directly or indirectly using the pointer register (pr) as the address pointer. the data space is accessible using the sld, sst, sin and sout instructions. the address pointer can be stored in the pointer register using the sldpr instruction. 18.4.1 message format each message comprises an instruction followed by one or more byte operands. the instruction is always sent by the external programmer. depending on the instruction all the following operands are sent either by the external programmer or by the tpi. the messages can be categorized in two types based on the instruction, as follows: z write messages. a write message is a request to write data. the write message is sent entirely by the external programmer. this message type is used with the sstcs, sst, stpr, sout and skey instructions. z read messages. a read message is a request to read data. the tpi reacts to the request by sending the byte operands. this message type is used with the sldcs, sld and sin instructions.
153 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 all the instructions except the skey instruction require the instruction to be followed by one byte operand. the skey instruction requires 8 byte operands. for more information, see the tpi instruction set on page 153 . 18.4.2 exception handling and synchronisation several situations are considered exceptions from normal operation of the tpi. when the tpi physical layer is in receive mode, these exceptions are: z the tpi physical layer detects a parity error. z the tpi physical layer detects a frame error. z the tpi physical layer recognizes a break character. when the tpi physical layer is in transmit mode, the possible exceptions are: z the tpi physical layer detects a data collision. all these exceptions are signalized to the tpi access layer. the access layer responds to an exception by aborting any on-going operation and enters the error state. the access layer will stay in the error state until a break character has been received, after which it is taken back to its default state. as a consequence, the external programmer can always synchronize the protocol by simply transmitting two successive break characters. 18.5 instruction set the tpi has a compact instruction set that is used to access the tpi control and status space (css) and the data space. the instructions allow the external programmer to access the tpi, the nvm controller and the nvm memories. all instructions except skey require one byte operand following the instruction. the skey instruction is followed by 8 data bytes. all instructions are byte-sized. the tpi instruction set is summarized in table 18-1 . table 18-1. instruction set summary mnemonic operand description operation sld data, pr serial load from data space using indirect addressing data ds[pr] sld data, pr+ serial load from data space using indirect addressing and post-increment data ds[pr] pr pr+1 sst pr, data serial store to data space using indirect addressing ds[pr] data sst pr+, data serial store to data space using indirect addressing and post-increment ds[pr] data pr pr+1 sstpr pr, a serial store to pointer register using direct addressing pr[a] data sin data, a serial in from data space data i/o[a] sout a, data serial out to data space i/o[a] data sldcs data, a serial load from control and status space using direct addressing data css[a] sstcs a, data serial store to control and status space using direct addressing css[a] data skey key, {8{data}} serial key key {8{data}}
154 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 18.5.1 sld - serial load from data space using indirect addressing the sld instruction uses indirect addressing to load data from the data space to the tpi physical layer shift-register for serial read-out. the data space location is pointed by the pointer register (pr), where the address must have been stored before data is accessed. the pointer register can either be left unchanged by the operation, or it can be post- incremented, as shown in table 18-2 . table 18-2. the serial load from data space (sld) instruction 18.5.2 sst - serial store to data space using indirect addressing the sst instruction uses indirect addressing to store into data space the byte that is shifted into the physical layer shift register. the data space location is pointed by the pointer register (pr), where the address must have been stored before the operation. the pointer register can be either left unchanged by the operation, or it can be post-incremented, as shown in table 18-3 . table 18-3. the serial store to data space (sld) instruction 18.5.3 sstpr - serial store to pointer register the sstpr instruction stores the data byte that is shifted into the physical layer shift register to the pointer register (pr). the address bit of the instruction specifies which byte of the pointer register is accessed, as shown in table 18-4 . table 18-4. the serial store to pointer register (sstpr) instruction 18.5.4 sin - serial in from i/o space using direct addressing the sin instruction loads data byte from the i/o space to the shift register of the physical layer for serial read-out. the instruction uses direct addressing, the address consisting of the 6 address bits of the instruction, as shown in table 18-5 . table 18-5. the serial in from i/o space (sin) instruction operation opcode remarks register data ds[pr] 0010 0000 pr pr unchanged data ds[pr] 0010 0100 pr pr + 1 post increment operation opcode remarks register ds[pr] data 0110 0000 pr pr unchanged ds[pr] data 0110 0100 pr pr + 1 post increment operation opcode remarks pr[a] data 0110 100a bit ?a? addresses pointer register byte operation opcode remarks data i/o[a] 0aa1 aaaa bits marked ?a? form the direct, 6-bit address
155 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 18.5.5 sout - serial out to i/o space using direct addressing the sout instruction stores the data byte that is shifted into the physical layer shift register to the i/o space. the instruction uses direct addressing, the address consisting of the 6 address bits of the instruction, as shown in table 18-6 . table 18-6. the serial out to i/o space (sout) instruction 18.5.6 sldcs - serial load data from control and status space using direct addressing the sldcs instruction loads data byte from the tpi control and status space to the tpi physical layer shift register for serial read-out. the sldcs instruction uses direct addressing, the direct address consisting of the 4 address bits of the instruction, as shown in table 18-7 . table 18-7. the serial load data from control and status space (sldcs) instruction 18.5.7 sstcs - serial store data to control and status space using direct addressing the sstcs instruction stores the data byte that is shifted into the tpi physical layer shift register to the tpi control and status space. the sstcs instruction uses direct addressing, the direct address consisting of the 4 address bits of the instruction, as shown in table 18-8 . table 18-8. the serial store data to control and status space (sstcs) instruction 18.5.8 skey - serial key signaling the skey instruction is used to signal the activation key that enables nvm programming. the skey instruction is followed by the 8 data bytes that includes the activation key, as shown in table 18-9 . table 18-9. the serial key signaling (skey) instruction 18.6 accessing the non-volatile memory controller by default, nvm programming is not enabled. in order to access the nvm controller and be able to program the non- volatile memories, a unique key must be sent using the skey instruction. the 64-bit key that will enable nvm programming is given in table 18-10 . operation opcode remarks i/o[a] data 1aa1 aaaa bits marked ?a? form the direct, 6-bit address operation opcode remarks data css[a] 1000 aaaa bits marked ?a? form the direct, 4-bit address operation opcode remarks css[a] data 1100 aaaa bits marked ?a? form the direct, 4-bit address operation opcode remarks key {8[data}} 1110 0000 data bytes follow after instruction
156 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 table 18-10. enable key for non-volatile memory programming after the key has been given, the non-volatile memory enable (nvmen) bit in the tpi status register (tpisr) must be polled until the non-volatile memory has been enabled. nvm programming is disabled by writing a logical zero to the nvmen bit in tpisr. 18.7 control and status space register descriptions the control and status registers of the tiny programming interface are mapped in the control and status space (css) of the interface. these registers are not part of the i/o register map and are accessible via sldcs and sstcs instructions, only. the control and status registers are directly involved in configuration and status monitoring of the tpi. a summary of css registers is shown in table 18-11 . table 18-11. summary of control and status registers key value nvm program enable 0x1289ab45cdd888ff addr. name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 0x0f tpiir tiny programming interface identification code 0x0e reserved ? ? ? ? ? ? ? ? 0x0d reserved ? ? ? ? ? ? ? ? 0x0c reserved ? ? ? ? ? ? ? ? 0x0b reserved ? ? ? ? ? ? ? ? 0x0a reserved ? ? ? ? ? ? ? ? 0x09 reserved ? ? ? ? ? ? ? ? 0x08 reserved ? ? ? ? ? ? ? ? 0x07 reserved ? ? ? ? ? ? ? ? 0x06 reserved ? ? ? ? ? ? ? ? 0x05 reserved ? ? ? ? ? ? ? ? 0x04 reserved ? ? ? ? ? ? ? ? 0x03 reserved ? ? ? ? ? ? ? ? 0x02 tpipcr ? ? ? ? ? gt2 gt1 gt0 0x01 reserved ? ? ? ? ? ? ? ? 0x00 tpisr ? ? ? ? ? ? nvmen ?
157 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 18.7.1 tpiir ? tiny programming interface identification register z bits 7:0 ? tpiic: tiny programming interface identification code these bits give the identification code for the tiny programming interface. the code can be used be the external programmer to identify the tpi. the identification code of the tiny programming interface is shown in table 18-12 . table 18-12. identification code for tiny programming interface 18.7.2 tpipcr ? tiny programming interface physical layer control register z bits 7:3 ? res: reserved bits these bits are reserved and will always read as zero. z bits 2:0 ? gt[2:0]: guard time these bits specify the number of additional idle bits that are inserted to the idle time when changing from reception mode to transmission mode. additional delays are not inserted when changing from transmission mode to reception. the total idle time when changing from reception to transmission mode is guard time plus two idle bits. table 18-13 shows the available guard time settings. table 18-13. guard time settings bit 76543210 css: 0x0f programming interface identification code tpiir read/write r r r r r r r r initial value 0 0 0 0 0 0 0 0 code value interface identification 0x80 bit 76543210 css: 0x02 ? ? ? ? ? gt2 gt1 gt0 tpipcr read/write r r r r r r/w r/w r/w initial value 0 0 0 0 0 0 0 0 gt2 gt1 gt0 guard time (number of idle bits) 0 0 0 +128 (default value) 0 0 1 +64 0 1 0 +32 0 1 1 +16 1 0 0 +8 1 0 1 +4 1 1 0 +2 1 1 1 +0
158 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 the default guard time is 128 idle bits. to speed up the communication, the guard time should be set to the shortest safe value. 18.7.3 tpisr ? tiny programming interface status register z bits 7:2, 0 ? res: reserved bits these bits are reserved and will always read as zero. z bit 1 ? nvmen: non-volatile memory programming enabled nvm programming is enabled when this bit is set. the external programmer can poll this bit to verify the interface has been successfully enabled. nvm programming is disabled by writing this bit to zero. bit 76543210 css: 0x00 ? ? ? ? ? ? nvmen ? tpipcr read/write r r r r r r r/w r initial value 0 0 0 0 0 0 0 0
159 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 19. memory programming 19.1 features z two embedded non-volatile memories: z non-volatile memory lock bits (nvm lock bits) z flash memory z four separate sections inside flash memory: z code section (program memory) z signature section z configuration section z calibration section z read access to all non-volatile memories from application software z read and write access to non-volatile memories from external programmer: z read access to all non-volatile memories z write access to nvm lock bits, flash code section and flash configuration section z external programming: z support for in-system and mass production programming z programming through the tiny programming interface (tpi) z high security with nvm lock bits 19.2 overview the non-volatile memory (nvm) controller manages all access to the non-volatile memories. the nvm controller controls all nvm timing and access privileges, and holds the status of the nvm. during normal execution the cpu will execute code from the code section of the flash memory (program memory). when entering sleep and no programming operations are active, the flash memory is disabled to minimize power consumption. all nvm are mapped to the data memory. application software can read the nvm from the mapped locations of data memory using load instruction with indirect addressing. the nvm has only one read port and, therefore, the next instruction and the data can not be read simultaneously. when the application reads data from nvm locations mapped to the data space, the data is read first before the next instruction is fetched. the cpu execution is here delayed by one system clock cycle. internal programming operations to nvm have been disabled and the nvm therefore appears to the application software as read-only. internal write or erase operations of the nvm will not be successful. the method used by the external programmer for writing the non-volatile memories is referred to as external programming. external programming can be done both in-system or in mass production. the external programmer can read and program the nvm via the tiny programming interface (tpi). in the external programming mode all nvm can be read and programmed, except the signature and the calibration sections which are read-only. nvm can be programmed at 5v, only. see table 20-9 on page 173 . 19.3 non-volatile memories the attiny20 has the following, embedded nvm: z non-volatile memory lock bits z flash memory with four separate sections
160 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 19.3.1 non-volatile memory lock bits the attiny20 provides two lock bits, as shown in table 19-1 . table 19-1. lock bit byte the lock bits can be left unprogrammed ("1") or can be programmed ("0") to obtain the additional security shown in table 19-2 . lock bits can be erased to "1" with the chip erase command, only. table 19-2. lock bit protection modes notes: 1. program the configuration section bits before programming nvlb1 and nvlb2. 2. "1" means unprogrammed, "0" means programmed 19.3.2 flash memory the embedded flash memory of attiny20 has four separate sections, as shown in table 19-3 . table 19-3. number of words and pages in the flash lock bit bit no description default value 7 1 (unprogrammed) 6 1 (unprogrammed) 5 1 (unprogrammed) 4 1 (unprogrammed) 3 1 (unprogrammed) 2 1 (unprogrammed) nvlb2 1 non-volatile lock bit 1 (unprogrammed) nvlb1 0 non-volatile lock bit 1 (unprogrammed) memory lock bits (1) protection type lock mode nvlb2 (2) nvlb1 (2) 1 1 1 no memory lock feature enabled 2 1 0 further programming of the flash memory is disabled in the external programming mode. the configuration section bits are locked in the external programming mode 3 0 0 further programming and verification of the flash is disabled in the external programming mode. the configuration section bits are locked in the external programming mode section size (bytes) page size (words) pages waddr paddr code (program memory) 2048 16 64 [4:1] [10:5]
161 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 notes: 1. this section is read-only. 19.3.3 configuration section attiny20 has one configuration byte, which resides in the configuration section. see table 19-4 . table 19-4. configuration bytes table 19-5 briefly describes the functionality of all configuration bits and how they are mapped into the configuration byte. table 19-5. configuration byte 0 notes: 1. see table 20-6 on page 171 for bodlevel fuse decoding. configuration bits are not affected by a chip erase but they can be cleared using the configuration section erase command (see ?erasing the configuration section? on page 165 ). note that configuration bits are locked if non-volatile lock bit 1 (nvlb1) is programmed. 19.3.3.1 latching of configuration bits all configuration bits are latched either when the device is reset or when the device exits the external programming mode. changes to configuration bit values have no effect until the device leaves the external programming mode. configuration 16 16 1 [4:1] ? signature (1) 32 16 2 [4:1] [5:5] calibration (1) 16 16 1 [4:1] ? section size (bytes) page size (words) pages waddr paddr configuration word address configuration word data high byte low byte 0x00 reserved configuration byte 0 0x01 ... 0x0f reserved reserved bit bit name description default value 7 ? reserved 1 (unprogrammed) 6 bodlevel2 (1) brown-out detector trigger level 1 (unprogrammed) 5 bodlevel1 (1) brown-out detector trigger level 1 (unprogrammed) 4 bodlevel0 (1) brown-out detector trigger level 1 (unprogrammed) 3 ? reserved 1 (unprogrammed) 2 ckout system clock output 1 (unprogrammed) 1 wdton watchdog timer always on 1 (unprogrammed) 0 rstdisbl external reset disable 1 (unprogrammed)
162 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 19.3.4 signature section the signature section is a dedicated memory area used for storing miscellaneous device information, such as the device signature. most of this memory section is reserved for internal use, as shown in table 19-6 . table 19-6. signature bytes attiny20 has a three-byte signature code, which can be used to identify the device. the three bytes reside in the signature section, as shown in table 19-6 . the signature data for attiny20 is given in table 19-7 . table 19-7. signature codes 19.3.5 calibration section attiny20 has one calibration byte. the calibration byte contains the calibration data for the internal oscillator and resides in the calibration section, as shown in table 19-8 . during reset, the calibration byte is automatically written into the osccal register to ensure correct frequency of the calibrated internal oscillator. table 19-8. calibration byte 19.3.5.1 latching of calibration value to ensure correct frequency of the calibrated internal oscillator the calibration value is automatically written into the osccal register during reset. 19.4 accessing the nvm nvm lock bits, and all flash memory sections are mapped to the data space as shown in figure 5-1 on page 15 . the nvm can be accessed for read and programming via the locations mapped in the data space. the nvm controller recognizes a set of commands that can be used to instruct the controller what type of programming task to perform on the nvm. commands to the nvm controller are issued via the nvm command register. see signature word address signature word data high byte low byte 0x00 device id 1 manufacturer id 0x01 reserved for internal use device id 2 0x02 ... 0x1f reserved for internal use reserved for internal use part signature bytes manufacturer id device id 1 device id 2 attiny20 0x1e 0x91 0x0f calibration word address calibration word data high byte low byte 0x00 reserved internal oscillator calibration value 0x01 ... 0x0f reserved reserved
163 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 ?nvmcmd ? non-volatile memory command register? on page 166 . after the selected command has been loaded, the operation is started by writing data to the nvm locations mapped to the data space. when the nvm controller is busy performing an operation it will signal this via the nvm busy flag in the nvm control and status register. see ?nvmcsr - non-volatile memory control and status register? on page 166 . the nvm command register is blocked for write access as long as the busy flag is active. this is to ensure that the current command is fully executed before a new command can start. programming any part of the nvm will automatically inhibit the following operations: z all programming to any other part of the nvm z all reading from any nvm location the attiny20 supports only external programming. internal programming operations to the nvm have been disabled, which means any internal attempt to write or erase nvm locations will fail. 19.4.1 addressing the flash the data space uses byte accessing but since the flash sections are accessed as words and organized in pages, the byte-address of the data space must be converted to the word-address of the flash section. this is illustrated in figure 19-1 . also, see table 19-3 on page 160 . figure 19-1. addressing the flash memory the most significant bits of the data space address select the nvm lock bits or the flash section mapped to the data memory. the word address within a page (waddr) is held by the bits [waddrmsb:1], and the page address (paddr) is held by the bits [paddrmsb:waddrmsb+1]. together, paddr and waddr form the absolute address of a word in the flash section. the least significant bit of the flash section address is used to select the low or high byte of the word. 19.4.2 reading the flash the flash can be read from the data memory mapped locations one byte at a time. for read operations, the least significant bit (bit 0) is used to select the low or high byte in the word address. if this bit is zero, the low byte is read, and if it is one, the high byte is read. 00 pag e 01 02 s ectionend word 00 01 pageend fla s h s ection fla s h pag e 0/1 waddr paddr 1 waddrm s b paddrm s b 16 addre ss pointer ... ... ... ... ... ... page address within a flash section word address within a flash pag e low/high byte select waddrm s b+1
164 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 19.4.3 programming the flash the flash can be written two words at a time. before writing a flash double word, the flash target location must be erased. writing to an un-erased flash word will corrupt its content. the flash is written two words at a time but the data space uses byte-addressing to access flash that has been mapped to data memory. it is therefore important to write the two words in the correct order to the flash, namely low bytes before high bytes. the low byte of the first word is first written to the temporary buffer, then the high byte. writing the low byte and then the high byte to the buffer latches the two words into the flash write buffer, starting the actual flash write operation. the flash erase operations can only performed for the entire flash sections. the flash programming sequence is as follows: 1. perform a flash section erase or perform a chip erase 2. write the flash section two words at a time 19.4.3.1 chip erase the chip erase command will erase the entire code section of the flash memory and the nvm lock bits. for security reasons, however, the nvm lock bits are not reset before the code section has been completely erased. the configuration, signature and calibration sections are not changed. before starting the chip erase, the nvmcmd register must be loaded with the chip_erase command. to start the erase operation a dummy byte must be written into the high byte of a word location that resides inside the flash code section. the nvmbsy bit remains set until erasing has been completed. while the flash is being erased neither flash buffer loading nor flash reading can be performed. the chip erase can be carried out as follows: 1. write the chip_erase command to the nvmcmd register 2. start the erase operation by writing a dummy byte to the high byte of any word location inside the code section 3. wait until the nvmbsy bit has been cleared 19.4.3.2 erasing the code section the algorithm for erasing all pages of the flash code section is as follows: 1. write the section_erase command to the nvmcmd register 2. start the erase operation by writing a dummy byte to the high byte of any word location inside the code section 3. wait until the nvmbsy bit has been cleared 19.4.3.3 writing a double code word the algorithm for writing two words to the code section is as follows: 1. write the dword_write command to the nvmcmd register 2. write the low byte of the low data word to the low byte of a target word location 3. write the high byte of the low data word to the high byte of the same target word location 4. send idle character as described in section ?supported characters? on page 150 5. write the low byte of the high data word to the low byte of the next target word location 6. write the high byte of the high data word to the high byte of the same target word location. this will start the flash write operation 7. wait until the nvmbsy bit has been cleared
165 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 19.4.3.4 erasing the configuration section the algorithm for erasing the configuration section is as follows: 1. write the section_erase command to the nvmcmd register 2. start the erase operation by writing a dummy byte to the high byte of any word location in the configuration section 3. wait until the nvmbsy bit has been cleared 19.4.3.5 writing a configuration word the algorithm for writing a configuration word is as follows. 1. write the dword_write command to the nvmcmd register 2. write the low byte of the data word to the low byte of the configuration word location 3. write the high byte of the data word to the high byte of the same configuration word location 4. send idle character as described in section ?supported characters? on page 150 5. write a dummy byte to the low byte of the next configuration word location 6. write a dummy byte to the high byte of the same configuration word location. this will start the flash write operation 7. wait until the nvmbsy bit has been cleared 19.4.4 reading nvm lock bits the non-volatile memory lock byte can be read from the mapped location in data memory. 19.4.5 writing nvm lock bits the algorithm for writing the lock bits is as follows. 1. write the dword_write command to the nvmcmd register. 2. write the lock bits value to the non-volatile memory lock byte location. this is the low byte of the non-volatile memory lock word. 3. start the nvm lock bit write operation by writing a dummy byte to the high byte of the nvm lock word location. 4. wait until the nvmbsy bit has been cleared. 19.5 self programming attiny20 doesn't support internal programming. 19.6 external programming the method for programming the non-volatile memories by means of an external programmer is referred to as external programming. external programming can be done both in-system or in mass production. the non-volatile memories can be externally programmed via the tiny programming interface (tpi). for details on the tpi, see ?programming interface? on page 148 . using the tpi, the external programmer can access the nvm control and status registers mapped to i/o space and the nvm memory mapped to data memory space. 19.6.1 entering external programming mode the tpi must be enabled before external programming mode can be entered. the following procedure describes, how to enter the external programming mode after the tpi has been enabled: 1. make a request for enabling nvm programming by sending the nvm memory access key with the skey instruction. 2. poll the status of the nvmen bit in tpisr until it has been set.
166 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 refer to the tiny programming interface description on page 148 for more detailed information of enabling the tpi and programming the nvm. 19.6.2 exiting external programming mode clear the nvm enable bit to disable nvm programming, then release the reset pin. see nvmen bit in ?tpisr ? tiny programming interface status register? on page 158 . 19.7 register description 19.7.1 nvmcmd ? non-volatile memory command register z bits 7:6 ? res: reserved bits these bits are reserved and will always read as zero. z bits 5:0 ? nvmcmd[5:0]: non-volatile memory command these bits define the programming commands for the flash, as shown in table 19-9 . table 19-9. nvm programming commands 19.7.2 nvmcsr - non-volatile memory control and status register z bit 7 ? nvmbsy: non-volatile memory busy this bit indicates the nvm memory (flash memory and lock bits) is busy, being programmed. this bit is set when a program operation is started, and it remains set until the operation has been completed. z bits 6:0 ? res: reserved bits these bits are reserved and will always read as zero. bit 76543210 0x33 ? ? nvmcmd[5:0] nvmcmd read/write r r r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 operation type nvmcmd mnemonic description binary hex general 0b000000 0x00 no_operation no operation 0b010000 0x10 chip_erase chip erase section 0b010100 0x14 section_erase section erase double word 0b011101 0x1d dword_write write double word bit 7 6543210 0x32 nvmbsy ? ? ? ???? nvmcsr read/write r/w rrrrrrr initial value 0 0 0 0 0000
167 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 20. electrical characteristics 20.1 absolute maximum ratings* 20.2 dc characteristics table 20-1. dc characteristics. t a = -40 to +85 c operating temperature . . . . . . . . . . . -55 c to +125 c *notice: stresses beyond those listed under ?absolute maximum ratings? may cause permanent damage to the device. this is a stress rating only and functional operation of the device at these or other conditions beyond those indicated in the opera- tional sections of this specification is not implied. exposure to absolute maximum rating conditions for extended periods may affect device reliability. storage temperature. . . . . . . . . . . . . -65 c to +150 c voltage on any pin except reset with respect to ground . . . . . . . . . .-0.5v to v cc +0.5v voltage on reset with respect to ground-0.5v to +13.0v maximum operating voltage . . . . . . . . . . . . . . . . 6.0v dc current per i/o pin . . . . . . . . . . . . . . . . . 40.0 ma dc current v cc and gnd pins . . . . . . . . . . 200.0 ma symbol parameter condition min typ (1) max units v il input low voltage v cc = 1.8v - 2.4v v cc = 2.4v - 5.5v -0.5 0.2v cc (3) 0.3v cc (3) v v ih input high-voltage except reset pin v cc = 1.8v - 2.4v v cc = 2.4v - 5.5v 0.7v cc (2) 0.6v cc (2) v cc +0.5 v input high-voltage reset pin v cc = 1.8v to 5.5v 0.9v cc (2) v cc +0.5 v v ol output low voltage (4) except reset pin (6) i ol = 10 ma, v cc = 5v i ol = 5 ma, v cc = 3v i ol = 2ma, v cc = 1.8v 0.6 0.5 0.4 v v oh output high-voltage (5) except reset pin (6) i oh = -10 ma, v cc = 5v i oh = -5 ma, v cc = 3v i oh = -2 ma, v cc = 1.8v 4.3 2.5 1.4 v i lil input leakage current i/o pin vcc = 5.5v, pin low (absolute value) <0.05 1 a i lih input leakage current i/o pin vcc = 5.5v, pin high (absolute value) <0.05 1 a r rst reset pull-up resistor vcc = 5.5v, input low 30 60 k r pu i/o pin pull-up resistor vcc = 5.5v, input low 20 50 k i aclk analog comparator input leakage current v cc = 5v v in = v cc /2 -50 50 na
168 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 notes: 1. typical values at 25 c. 2. ?min? means the lowest value where the pin is guaranteed to be read as high. 3. ?max? means the highest value where the pin is guaranteed to be read as low. 4. although each i/o port can sink more than the test conditions (10 ma at v cc = 5v, 5 ma at v cc = 3v) under steady state conditions (non-transient), the sum of all i ol (for all ports) should not exceed 100 ma. if i ol exceeds the test conditions, v ol may exceed the related specification. pins are not guaranteed to sink cur- rent greater than the listed test condition. 5. although each i/o port can source more than the test conditions (10 ma at v cc = 5v, 5 ma at v cc = 3v) under steady state conditions (non-transient), the sum of all i oh (for all ports) should not exceed 100 ma. if i oh exceeds the test condition, v oh may exceed the related specification. pins are not guaranteed to source current greater than the listed test condition. 6. the reset pin must tolerate high voltages when entering and operating in programming modes and, as a consequence, has a weak drive strength as compared to regular i/o pins. see figure 21-30 on page 190 , and figure 21-33 on page 191 . 7. values are with external clock using methods described in ?minimizing power consumption? on page 25 . power reduction is enabled (prr = 0xff) and there is no i/o drive. 8. bod disabled. 20.3 speed the maximum operating frequency of the device is dependent on supply voltage, v cc . the relationship between supply voltage and maximum operating frequency is piecewise linear, as shown in figure 20-1 , the maximum frequency vs. v cc curve is linear between 1.8v < v cc < 4.5v. i cc power supply current (7) active 1 mhz, v cc = 2v 0.2 0.6 ma active 4 mhz, v cc = 3v 1.1 2 ma active 8 mhz, v cc = 5v 3.2 5 ma idle 1 mhz, v cc = 2v 0.03 0.2 ma idle 4 mhz, v cc = 3v 0.2 0.5 ma idle 8 mhz, v cc = 5v 0.8 1.5 ma power-down mode (8) wdt enabled, v cc = 3v 4.5 10 a wdt disabled, v cc = 3v 0.15 2 a symbol parameter condition min typ (1) max units
169 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 20-1. maximum operating frequency vs. supply voltage 20.4 clock characteristics 20.4.1 accuracy of calibrated internal oscillator it is possible to manually calibrate the internal oscillator to be more accurate than default factory calibration. note that th e oscillator frequency depends on temperature and voltage. voltage and temperature characteristics can be found in figure 21-53 on page 201 and figure 21-54 on page 202 . table 20-2. calibration accuracy of internal rc oscillator note: 1. accuracy of oscillator frequency at calibration point (fixed temperature and fixed voltage). 20.4.2 external clock drive figure 20-2. external clock drive waveform 4 mhz 1. 8 v 5.5v 4.5v 12 mhz 2.7v 8 mhz calibration method target frequency v cc temperature accuracy at given voltage & temperature (1) factory calibration 8.0 mhz 3v 25 c 10% user calibration fixed frequency within: 7.3 ? 8.1 mhz fixed voltage within: 1.8v ? 5.5v fixed temp. within: -40 c to +85 c 1% v il1 v ih1
170 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 table 20-3. external clock drive characteristics 20.5 system and reset characteristics table 20-4. reset and internal voltage characteristics note: 1. values are guidelines, only 20.5.1 power-on reset table 20-5. characteristics of enhanced power-on reset. t a = -40 to +85 c note: 1. values are guidelines, only 2. threshold where device is released from reset when voltage is rising 3. the power-on reset will not work unless the supply voltage has been below v pot (falling) symbol parameter v cc = 1.8 - 5.5v v cc = 2.7 - 5.5v v cc = 4.5 - 5.5v units min. max. min. max. min. max. 1/t clcl clock frequency 0 4 0 8 0 12 mhz t clcl clock period 250 125 83 ns t chcx high time 100 40 20 ns t clcx low time 100 40 20 ns t clch rise time 2.0 1.6 0.5 s t chcl fall time 2.0 1.6 0.5 s t clcl change in period from clock cycle to next 2 2 2 % symbol parameter condition min (1) typ (1) max (1) units v rst reset pin threshold voltage 0.2 v cc 0.9v cc v v bg internal bandgap voltage v cc = 2.7v t a = 25c 1.0 1.1 1.2 v t rst minimum pulse width on reset pin v cc = 1.8v v cc = 3v v cc = 5v 2000 700 400 ns t tout time-out after reset bod disabled 64 128 ms bod enabled 128 256 symbol parameter min (1) typ (1) max (1) units v por release threshold of power-on reset (2) 1.1 1.4 1.6 v v poa activation threshold of power-on reset (3) 0.6 1.3 1.6 v sr on power-on slope rate 0.01 v/ms
171 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 20.5.2 brown-out detection table 20-6. vbot vs. bodlevel fuse coding note: 1. v bot may be below nominal minimum operating voltage for some devices. for devices where this is the case, the device is tested down to v cc = v bot during the production test. this guarantees that a brown-out reset will occur before v cc drops to a voltage where correct operation of the microcontroller is no longer guaranteed. 20.6 analog comparator characteristics table 20-7. analog comparator characteristics, ta = -40 to +85 c bodlevel[2:0] fuses min (1) typ (1) max (1) units 111 bod disabled 110 1.7 1.8 2.0 v 101 2.5 2.7 2.9 100 4.1 4.3 4.5 0xx reserved symbol parameter condition min typ max units v aio input offset voltage v cc = 5v, vin = v cc / 2 < 10 40 mv i lac input leakage current v cc = 5v, vin = v cc / 2 -50 50 na t dpd digital propagation delay v cc = 1.8v - 5.5 1 2 clk t apd analog propagation delay (from saturation to slight overdrive) v cc = 2.7v 750 ns v cc = 4.0v 500 analog propagation delay (large step change) v cc = 2.7v 100 v cc = 4.0v 75
172 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 20.7 adc characteristics table 20-8. adc characteristics. t a = -40 to +85 c. v cc = 1.8 ? 5.5v symbol parameter condition min typ max units resolution 10 bits absolute accuracy (including inl, dnl, and quantization, gain and offset errors) v ref = v cc = 4v, adc clock = 200 khz 2 lsb v ref = v cc = 4v, adc clock = 1 mhz 3 lsb v ref = v cc = 4v, adc clock = 200 khz noise reduction mode 1.5 lsb v ref = v cc = 4v, adc clock = 1 mhz noise reduction mode 2.5 lsb integral non-linearity (inl) (accuracy after offset and gain calibration) v ref = v cc = 4v, adc clock = 200 khz 1 lsb differential non-linearity (dnl) v ref = v cc = 4v, adc clock = 200 khz 0.5 lsb gain error v ref = v cc = 4v, adc clock = 200 khz 2.5 lsb offset error v ref = v cc = 4v, adc clock = 200 khz 1.5 lsb conversion time free running conversion 13 260 s clock frequency 50 1000 khz v in input voltage gnd v ref v input bandwidth 38.5 khz r ain analog input resistance 100 m adc conversion output 0 1023 lsb
173 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 20.8 serial programming characteristics figure 20-3. serial programming timing table 20-9. serial programming characteristics symbol parameter min typ max units v cc programming voltage 4.75 5 5.5 v f clcl clock frequency 2 mhz t clcl clock period 500 ns t clch clock low pulse width 200 ns t chch clock high pulse width 200 ns t ivch data input to clock high setup time 50 ns t chix data input hold time after clock high 100 ns t clov data output valid after clock low time 200 ns t chix t pid ata t ivch t chcl t clc h t clcl t pic lk t clo v transmit mode rec eive mode
174 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 21. typical characteristics the data contained in this section is largely based on simulations and characterization of similar devices in the same process and design methods. thus, the data should be treated as indications of how the part will behave. the following charts show typical behavior. these figures are not tested during manufacturing. during characterisation devices are operated at frequencies higher than test limits but they are not guaranteed to function properly at frequencies higher than the ordering code indicates. all current consumption measurements are performed with all i/o pins configured as inputs and with internal pull-ups enabled. current consumption is a function of several factors such as operating voltage, operating frequency, loading of i/o pins, switching rate of i/o pins, code executed and ambient temperature. the dominating factors are operating voltage and frequency. a sine wave generator with rail-to-rail output is used as clock source but current consumption in power-down mode is independent of clock selection. the difference between current consumption in power-down mode with watchdog timer enabled and power-down mode with watchdog timer disabled represents the differential current drawn by the watchdog timer. the current drawn from pins with a capacitive load may be estimated (for one pin) as follows: where v cc = operating voltage, c l = load capacitance and f sw = average switching frequency of pin. 21.1 supply current of i/o modules the tables and formulas below can be used to calculate the additional current consumption for the different i/o modules in active and idle mode. the enabling or disabling of the i/o modules is controlled by the power reduction register. see ?power reduction register? on page 24 for details. table 21-1. additional current consumption for different i/o modules (absolute values) table 21-2 below can be used for calculating typical current consumption for other supply voltages and frequencies than those mentioned in the table 21-1 above. i cp v cc c l f sw prr bit typical numbers prtim0 4 a 25 a 110 a prtim1 5 a 35 a 150 a pradc 190 a 260 a 470 a prspi 3 a 15 a 75 a prtwi 5 a 35 a 160 a
175 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 table 21-2. additional current consumption (percentage) in active and idle mode 21.2 current consumption in active mode figure 21-1. active supply current vs. low frequency (0.1 - 1.0 mhz) prr bit current consumption additional to active mode with external clock (see figure 21-1 and figure 21-2 ) current consumption additional to idle mode with external clock (see figure 21-6 and figure 21-7 ) prtim0 2% 15% prtim1 3% 20% pradc see figure 21-14 on page 182 see figure 21-14 on page 182 prtspi 2% 10% prttwi 4% 20% active supply current vs. low frequency 5.5 v 5.0 v 4.5 v 4.0 v 3.3 v 2.7 v 1.8 v 0 0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 0,9 0 0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 0,9 1 frequency [mhz] icc [ma]
176 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 21-2. active supply current vs. frequency (1 - 12 mhz) figure 21-3. active supply current vs. v cc (internal oscillator, 8 mhz) 5.5 v 5.0 v 4.5 v 4.0 v 3.3 v 2.7 v 1.8 v active supply current vs. frequency 0 0,5 1 1,5 2 2,5 3 3,5 4 4,5 5 5,5 6 2 1 0 1 8 6 4 2 0 frequency [mhz] icc [ma] active supply current vs. v cc internal rc oscillator, 8 mhz 0 0,5 1 1,5 2 2,5 3 3,5 4 1,5 2 2,5 3 3,5 4 4,5 5 vcc [v] icc [ma] 85 c 25 c -40 c
177 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 21-4. active supply current vs. v cc (internal oscillator, 1 mhz) figure 21-5. active supply current vs. v cc (internal oscillator, 128 khz) 85 c 25 c -40 c active supply current vs. v cc internal rc oscillator, 1 mhz 0 0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 0,9 1 1,5 2 2,5 3 3,5 4 4,5 5 5,5 vcc [v] icc [ma] active supply current vs. v cc internal rc oscillator, 128 khz 0 0,02 0,04 0,06 0,08 0,1 0,12 1,5 2 2,5 3 3,5 4 4,5 5 5,5 vcc [v] icc [ma] 85 c 25 c -40 c
178 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 21.3 current consumption in idle mode figure 21-6. idle supply current vs. low frequency (0.1 - 1.0 mhz) figure 21-7. idle supply current vs. frequency (1 - 12 mhz) idle supply current vs. low frequency 5.5 v 5.0 v 4.5 v 4.0 v 3.3 v 2.7 v 1.8 v 0 0,02 0,04 0,06 0,08 0,1 0,12 0 0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 0,9 1 frequency [mhz] icc [ma] 5.5 v 5.0 v 4.5 v 4.0 v 3.3 v 2.7 v 1.8 v idle supply current vs. frequency 0 0,2 0,4 0,6 0,8 1 1,2 1,4 2 1 0 1 8 6 4 2 0 frequency [mhz] icc [ma]
179 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 21-8. idle supply current vs. v cc (internal oscillator, 8 mhz) figure 21-9. idle supply current vs. v cc (internal oscillator, 1 mhz) idle supply current vs. v cc internal rc oscillator, 8 mhz 0 0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 0,9 1 1,5 2 2,5 3 3,5 4 4,5 5 5,5 vcc [v] icc [ma] 85 c 25 c -40 c idle supply current vs. v cc internal rc oscillator, 1 mhz 0 0,05 0,1 0,15 0,2 0,25 1,5 2 2,5 3 3,5 4 4,5 5 5,5 vcc [v] icc [ma] 85 c 25 c -40 c 85 c 25 c -40 c
180 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 21-10.idle supply current vs. v cc (internal oscillator, 128 khz) 21.4 current consumption in power-down mode figure 21-11.power-down supply current vs. v cc (watchdog timer disabled) idle supply current vs. v cc internal rc oscillator, 128 khz 0 0,005 0,01 0,015 0,02 0,025 0,03 1,5 2 2,5 3 3,5 4 4,5 5 5,5 vcc [v] icc [ma] 85 c 25 c -40 c 85 c 25 c -40 c power-down supply current vs. v cc watchdog timer disabled 0 0,05 0,1 0,15 0,2 0,25 0,3 0,35 0,4 0,45 1,5 2 2,5 3 3,5 4 4,5 5 5,5 vcc [v] icc [ua] 85 c 25 c -40 c
181 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 21-12.power-down supply current vs. v cc (watchdog timer enabled) 21.5 current consumption in reset figure 21-13.reset supply current vs. v cc (excluding current through the reset pull-up and no clock) 85 c 25 c -40 c power-down supply current vs. v cc watchdog timer enabled 0 1 2 3 4 5 6 7 8 9 10 1,5 2 2,5 3 3,5 4 4,5 5 5,5 vcc [v] icc [ua] reset current vs. v cc excluding current through the reset pullup and no clock 0 0,2 0,4 0,6 0,8 1 1,2 1,5 2 2,5 3 3,5 4 4,5 5 5,5 vcc [v] icc [ma] 25 c 85 c -40 c
182 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 21.6 current consumption of peripheral units figure 21-14.adc current vs. v cc (at clk adc = 250khz) figure 21-15.analog comparator current vs. v cc adc current vs. v cc 0 50 100 150 200 250 300 350 400 450 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc [v] i cc [ua] analog comparator current vs. v cc 0 10 20 30 40 50 60 70 80 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc [v] i cc [ua]
183 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 21-16.watchdog timer current vs. v cc figure 21-17.brownout detector current vs. v cc watchdog timer current vs. v cc 0 1 2 3 4 5 6 7 8 9 10 1,5 2 2,5 3 3,5 4 4,5 5 5,5 vcc [v] icc [ua] 85 c 25 c -40 c brownout detector current vs. v cc 0 5 10 15 20 25 30 35 40 45 1,522,533,544,555,5 vcc [v] icc [ua] 85 c 25 c -40 c
184 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 21.7 pull-up resistors figure 21-18.i/o pin pull-up resistor current vs. input voltage (v cc = 1.8v) figure 21-19.i/o pin pull-up resistor current vs. input voltage (v cc = 2.7v) i/o pin pull-up resistor current vs. input voltage 0 10 20 30 40 50 60 0 0,2 0,4 0,6 0,8 1 1,2 1,4 1,6 1,8 2 vop [v] iop [ua] 85 c 25 c -40 c 85 c 25 c -40 c i/o pin pull-up resistor current vs. input voltage 0 10 20 30 40 50 60 70 80 3 5 , 2 2 5 , 1 1 5 , 0 0 vop [v] iop [ua]
185 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 21-20.i/o pin pull-up resistor current vs. input voltage (v cc = 5v) figure 21-21.reset pull-up resistor current vs. reset pin voltage (v cc = 1.8v) 85 c 25 c -40 c i/o pin pull-up resistor current vs. input voltage 0 20 40 60 80 100 120 140 160 6 5 4 3 2 1 0 vop [v] iop [ua] 25 c 85 c -40 c reset pull-up resistor current vs. reset pin voltage 0 5 10 15 20 25 30 35 40 0 0,2 0,4 0,6 0,8 1 1,2 1,4 1,6 1,8 2 vreset [v] ireset [ua]
186 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 21-22.reset pull-up resistor current vs. reset pin voltage (v cc = 2.7v) figure 21-23.reset pull-up resistor current vs. reset pin voltage (v cc = 5v) 25 c 85 c -40 c reset pull-up resistor current vs. reset pin voltage 0 10 20 30 40 50 60 3 5 , 2 2 5 , 1 1 5 , 0 0 vreset [v] ireset [ua] 25 c 85 c -40 c reset pull-up resistor current vs. reset pin voltage 0 20 40 60 80 100 120 6 5 4 3 2 1 0 vreset [v] ireset [ua]
187 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 21.8 output driver strength figure 21-24.v ol : output voltage vs. sink current (i/o pin, v cc = 1.8v) figure 21-25.v ol : output voltage vs. sink current (i/o pin, v cc = 3v) 85 c 25 c 85 c 25 c -40 c i/o pin output voltage vs. sink current v cc = 1.8v 0 0,2 0,4 0,6 0,8 0 0,5 1 1,5 2 2,5 3 3,5 4 4,5 5 i ol [ma] v ol [v] 85 c 85 c 25 c -40 c i/o pin output voltage vs. sink current v cc = 3v 0 0,2 0,4 0,6 0,8 012345678910 i ol [ma] v ol [v]
188 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 21-26.v ol : output voltage vs. sink current (i/o pin, v cc = 5v) figure 21-27.v oh : output voltage vs. source current (i/o pin, v cc = 1.8v) 85 c 85 c 25 c -40 c i/o pin output voltage vs. sink current v cc = 5v 0 0,2 0,4 0,6 0,8 1 0 2 4 6 8 101214161820 i ol [ma] v ol [v] 85 c 25 c -40 c i/o pin output voltage vs. source current v cc = 1.8v 1 1,1 1,2 1,3 1,4 1,5 1,6 1,7 1,8 1,9 3 5 , 2 2 5 , 1 1 5 , 0 0 i oh [ma] v oh [v]
189 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 21-28.v oh : output voltage vs. source current (i/o pin, v cc = 3v) figure 21-29.v oh : output voltage vs. source current (i/o pin, v cc = 5v) -40 c 85 c 25 c i/o pin output voltage vs. source current v cc = 3v 1,6 1,8 2 2,2 2,4 2,6 2,8 3 3,2 012345678910 i oh [ma] v oh [v] -40 c 85 c 25 c i/o pin output voltage vs. source current v cc = 5v 3,8 4 4,2 4,4 4,6 4,8 5 5,2 0 2 4 6 8 101214161820 i oh [ma] v oh [v]
190 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 21-30.v ol : output voltage vs. sink current (reset pin as i/o, v cc = 1.8v) figure 21-31.v ol : output voltage vs. sink current (reset pin as i/o, v cc = 3v) 25 c 85 c -40 c reset as i/o output voltage vs. sink current vcc = 1.8v 0 0,2 0,4 0,6 0,8 1 6 , 0 5 , 0 4 , 0 3 , 0 2 , 0 1 , 0 0 iol [ma] vol [v] 25 c 85 c -40 c reset as i/o output voltage vs. sink current vcc = 3v 0 0,2 0,4 0,6 0,8 1 1,2 1,4 1,6 1,8 2 3 5 , 2 2 5 , 1 1 5 , 0 0 iol [ma] vol [v]
191 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 21-32.v ol : output voltage vs. sink current (reset pin as i/o, v cc = 5v) figure 21-33.v oh : output voltage vs. source current (reset pin as i/o, v cc = 1.8v 25 c 85 c -40 c reset as i/o output voltage vs. sink current vcc = 5v 0 0,2 0,4 0,6 0,8 1 1,2 1,4 1,6 1,8 2 0 0,5 1 1,5 2 2,5 3 3,5 4 4,5 5 iol [ma] vol [v] 25 c 85 c -40 c reset as i/o output voltage vs. source current vcc = 1.8v 0 0,2 0,4 0,6 0,8 1 1,2 1,4 1,6 0 0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 0,9 1 ioh [ma] voh [v]
192 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 21-34.v oh : output voltage vs. source current (reset pin as i/o, v cc = 3v figure 21-35.v oh : output voltage vs. source current (reset pin as i/o, v cc = 5v 25 c 85 c -40 c reset as i/o output voltage vs. source current vcc = 3v 0 0,5 1 1,5 2 2,5 3 0 0,2 0,4 0,6 0,8 1 1,2 1,4 1,6 1,8 ioh [ma] voh [v] 25 c 85 c -40 c reset as i/o output voltage vs. source current vcc = 5v 0 0,5 1 1,5 2 2,5 3 3,5 4 4,5 0 0,2 0,4 0,6 0,8 1 1,2 1,4 1,6 1,8 ioh [ma] voh [v]
193 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 21.9 input thresholds and hysteresis figure 21-36.v ih : input threshold voltage vs. v cc (i/o pin, read as ?1?) figure 21-37.v il : input threshold voltage vs. v cc (i/o pin, read as ?0?) i/o pin input threshold voltage vs. v cc vih, io pin read as '1' 0 0,5 1 1,5 2 2,5 3 3,5 1,5 2 2,5 3 3,5 4 4,5 5 5,5 vcc [v] threshold [v] 85 c 25 c -40 c 85 c 25 c -40 c 25 c 85 c -40 c reset input threshold voltage vs. v cc vil, chip reset 0 0,5 1 1,5 2 2,5 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc [v] threshold [v]
194 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 21-38.v ih -v il : input hysteresis vs. v cc (i/o pin) figure 21-39.v ih : input threshold voltage vs. v cc (reset pin as i/o, read as ?1?) 25 c 85 c -40 c i/o pin input hysteresis 0 0,1 0,2 0,3 0,4 0,5 0,6 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc [v] hysteresis [v] reset pin as i/o threshold voltage vs. v cc vih, reset read as '1' 0 0,5 1 1,5 2 2,5 3 3,5 1,5 2 2,5 3 3,5 4 4,5 5 5,5 vcc [v] threshold [v] 25 c 85 c -40 c
195 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 21-40.v il : input threshold voltage vs. v cc (reset pin as i/o, read as ?0?) figure 21-41.v ih -v il : input hysteresis vs. v cc (reset pin as i/o) 25 c 85 c -40 c reset pin as i/o threshold voltage vs. v cc vil, reset read as '0' 0 0,5 1 1,5 2 2,5 1,5 2 2,5 3 3,5 4 4,5 5 5,5 vcc [v] threshold [v] 25 c 85 c -40 c reset as i/o input hysteresis 0 0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 1,5 2 2,5 3 3,5 4 4,5 5 5,5 vcc [v] hysteresis [v]
196 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 21.10 bod, bandgap and reset figure 21-42.bod threshold vs temperature (bodlevel is 4.3v) figure 21-43.bod threshold vs temperature (bodlevel is 2.7v) v cc rising v cc falling bod thresholds vs. temperature bod level 4.3 v 4,18 4,2 4,22 4,24 4,26 4,28 4,3 4,32 4,34 -40-20 0 20406080100 temperature [c] threshold [v] v cc rising v cc falling bod thresholds vs. temperature bod level 2.7 v 2,64 2,65 2,66 2,67 2,68 2,69 2,7 2,71 2,72 2,73 2,74 2,75 -40 -20 0 20 40 60 80 100 temperature [c] threshold [v]
197 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 21-44.bod threshold vs temperature (bodlevel is 1.8v) figure 21-45.bandgap voltage vs. supply voltage bod thresholds vs. temperature bod level 1.8 v v cc rising 1,77 1,775 1,78 1,785 1,79 1,795 1,8 1,805 1,81 1,815 -40 -20 0 20 40 60 80 100 temperature [c] threshold [v] v cc falling 25 c 85 c -40 c bandgap voltage vs. vcc 1 1,01 1,02 1,03 1,04 1,05 1,06 1,07 1,08 1,09 1,1 1,5 2 2,5 3 3,5 4 4,5 5 5,5 vcc [v] reference [v]
198 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 21-46.v ih : input threshold voltage vs. v cc (reset pin, read as ?1?) figure 21-47.v il : input threshold voltage vs. v cc (reset pin, read as ?0?) reset input threshold voltage vs. v cc vih, reset released 0 0,5 1 1,5 2 2,5 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc [v] threshold [v] 25 c 85 c -40 c 25 c 85 c -40 c reset input threshold voltage vs. v cc vil, chip reset 0 0,5 1 1,5 2 2,5 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc [v] threshold [v]
199 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 21-48.v ih -v il : input hysteresis vs. v cc (reset pin) figure 21-49.minimum reset pulse width vs. v cc 25 c 85 c -40 c reset input hysteresis 0 0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 0,9 1 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc [v] hysteresis [v] minimum reset pulse width vs. v cc 85 25 -40 0 500 1000 1500 2000 2500 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc [v] pulsewidth [ns]
200 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 21.11 analog comparator offset figure 21-50.analog comparator offset 21.12 internal oscillator speed figure 21-51.watchdog oscillator frequency vs. v cc 85 c 25 c -40 c analog comparator offset v cc = 5v -0,016 -0,014 -0,012 -0,01 -0,008 -0,006 -0,004 -0,002 0 0,002 0 0,5 1 1,5 2 2,5 3 3,5 4 4,5 5 v in (v) offset (v) -40 c 85 c 25 c watchdog oscillator frequency vs. operating voltage 0,098 0,099 0,1 0,101 0,102 0,103 0,104 0,105 0,106 0,107 0,108 1,5 2 2,5 3 3,5 4 4,5 5 5,5 vcc [v] frc [mhz]
201 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 21-52.watchdog oscillator frequency vs. temperature figure 21-53.calibrated oscillator frequency vs. v cc watchdog oscillator frequency vs. temperature 5.5 v 4.0 v 3.5 v 2.8 v 1.8 v 0,106 0,108 0,11 0,112 0,114 0,116 0,118 -40 -20 0 20 40 60 80 100 temperature [c] frc [mhz] 85 c 25 c -40 c calibrated 8mhz rc oscillator frequency vs. operating voltage 7,4 7,6 7,8 8 8,2 8,4 1,5 2 2,5 3 3,5 4 4,5 5 5,5 vcc [v] frc [mhz]
202 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 figure 21-54.calibrated oscillator frequency vs. temperature figure 21-55.calibrated oscillator frequency vs. osccal value calibrated 8mhz rc oscillator frequency vs. temperature 5.0 v 3.0 v 1.8 v 7,6 7,7 7,8 7,9 8 8,1 8,2 8,3 -40 -20 0 20 40 60 80 100 temperature [c] frc [mhz] calibrated 8.0mhz rc oscillator frequency vs. osccal value 0 2 4 6 8 10 12 14 16 0 16 32 48 64 80 96 112 128 144 160 176 192 208 224 240 osccal [x1] frc [mhz] 85 c 25 c -40 c
203 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 22. register summary address name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 page 0x3f sreg i t h s v n z c page 14 0x3e sph stack pointer high byte page 13 0x3d spl stack pointer low byte page 13 0x3c ccp cpu change protection byte page 13 0x3b rstflr ? ? ? ? wdrf borf extrf porf page 35 0x3a mcucr icsc01 icsc00 ? bods sm2 sm1 sm0 se pages 26 , 38 0x39 osccal oscillator calibration byte page 22 0x38 reserved ? ? ? ? ? ? ? ? 0x37 clkmsr ? ? ? ? ? ? clkms1 clkms0 page 20 0x36 clkpsr ? ? ? ? clkps3 clkps2 clkps1 clkps0 page 21 0x35 prr ? ? ? prtwi prspi prtim1 prtim0 pradc page 27 0x34 qtcsr qtouch control and status register page 7 0x33 nvmcmd ? ? nvm command page 166 0x32 nvmcsr nvmbsy ? ? ? ? ? ? ? page 166 0x31 wdtcsr wdif wdie wdp3 ? wde wdp2 wdp1 wdp0 page 33 0x30 spcr spie spe dord mstr cpol cpha spr1 spr0 page 132 0x2f spsr spif wcol ? ? ? ? ssps spi2x page 133 0x2e spdr spi data register page 134 0x2d twscra twshe ? twdie twasie twen twsie twpme twsme page 143 0x2c twscrb ? ? ? ? ? twaa twcmd[1.0] page 144 0x2b twssra twdif twasif twch twra twc twbe twdir twas page 145 0x2a twsa twi slave address register page 146 0x29 twsam twi slave address mask register page 147 0x28 twsd twi slave data register page 146 0x27 gtccr tsm ? ? ? ? ? ? psr page 104 0x26 timsk ice1 ? ocie1b ocie1a toie1 ocie0b ocie0a toie0 pages 74 , 101 0x25 tifr icf1 ? ocf1b ocf1a tov1 ocf0b ocf0a tov0 pages 75 , 102 0x24 tccr1a com1a1 com1a0 com1b1 com1b0 ? ? wgm11 wgm10 page 96 0x23 tccr1b icnc1 ices1 ? wgm13 wgm12 cs12 cs11 cs10 page 98 0x22 tccr1c foc1a foc1b ? ? ? ? ? ? page 100 0x21 tcnt1h timer/counter1 ? counter register high byte page 100 0x20 tcnt1l timer/counter1 ? counter register low byte page 100 0x1f ocr1ah timer/counter1 ? compare register a high byte page 100 0x1e ocr1al timer/counter1 ? compare register a low byte page 100 0x1d ocr1bh timer/counter1 ? compare register b high byte page 101 0x1c ocr1bl timer/counter1 ? compare register b low byte page 101 0x1b icr1h timer/counter1 - input capture register high byte page 101 0x1a icr1l timer/counter1 - input capture register low byte page 101 0x19 tccr0a com0a1 com0a0 com0b1 com0b0 ? ? wgm01 wgm00 page 69 0x18 tccr0b foc0a foc0b ? ? wgm02 cs02 cs01 cs00 page 72 0x17 tcnt0 timer/counter0 ? counter register page 73 0x16 ocr0a timer/counter0 ? compare register a page 74 0x15 ocr0b timer/counter0 ? compare register b page 74 0x14 acsra acd acbg aco aci acie acic acis1 acis0 page 106 0x13 acsrb hsel hlev aclp ? acce acme acirs1 acirs0 page 107 0x12 adcsra aden adsc adate adif adie adps2 adps1 adps0 page 122 0x11 adcsrb vden vdpd ? ? adlar adts2 adts1 adts0 page 123 0x10 admux ? refs refen adc0en mux3 mux2 mux1 mux0 page 120 0x0f adch adc conversion result ? high byte page 121 0x0e adcl adc conversion result ? low byte page 121 0x0d didr0 adc7d adc6d adc5d adc4d adc3d adc2d adc1d adc0d page 124 0x0c gimsk ? ? pcie1 pcie0 ? ? ? int0 page 39 0x0b gifr ? ? pcif1 pcif0 ? ? ? intf0 page 40 0x0a pcmsk1 ? ? ? ? pcint11 pcint10 pcint9 pcint8 page 40 0x09 pcmsk0 pcint7 pcint6 pcint5 pcint4 pcint3 pcint2 pcint1 pcint0 page 41 0x08 portcr ? ? ? ? ? ? bbmb bbma page 56 0x07 pueb ? ? ? ? pueb3 pueb2 pueb1 pueb0 page 57 0x06 portb ? ? ? ? portb3 portb2 portb1 portb0 page 57 0x05 ddrb ? ? ? ? ddrb3 ddrb2 ddrb1 ddrb0 page 57 0x04 pinb ? ? ? ? pinb3 pinb2 pinb1 pinb0 page 58 0x03 puea puea7 puea6 puea5 puea4 puea3 puea2 puea1 puea0 page 57 0x02 porta porta7 porta6 porta5 porta4 porta3 porta2 porta1 porta0 page 57 0x01 ddra ddra7 ddra6 ddra5 ddra4 ddra3 ddra2 ddra1 ddra0 page 57 0x00 pina pina7 pina6 pina5 pina4 pina3 pina2 pina1 pina0 page 57
204 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 note: 1. for compatibility with future devices, reserved bits should be written to zero if accessed. reserved i/o memory addresses should never be written. 2. i/o registers within the address range 0x00 - 0x1f are directly bit-accessible using the sbi and cbi instructions. in these registers, the value of single bits can be checked by using the sbis and sbic instructions. 3. some of the status flags are cleared by writing a logical one to them. note that, unlike most other avrs, the cbi and sbi instructions will only operation the specified bit, and can therefore be used on registers containing such status flags. the cbi and sbi instructions work with registers 0x00 to 0x1f only.
205 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 23. instruction set summary mnemonics operands description operation flags #clocks arithmetic and logic instructions add rd, rr add without carry rd rd + rr z,c,n,v,s,h 1 adc rd, rr add with carry rd rd + rr + c z,c,n,v,s,h 1 sub rd, rr subtract without carry rd rd - rr z,c,n,v,s,h 1 subi rd, k subtract immediate rd rd - k z,c,n,v,s,h 1 sbc rd, rr subtract with carry rd rd - rr - c z,c,n,v,s,h 1 sbci rd, k subtract immediate with carry rd rd - k - c z,c,n,v,s,h 1 and rd, rr logical and rd rd ? rr z,n,v,s 1 andi rd, k logical and with immediate rd rd ? k z,n,v,s 1 or rd, rr logical or rd rd v rr z,n,v,s 1 ori rd, k logical or with immediate rd rd v k z,n,v,s 1 eor rd, rr exclusive or rd rd rr z,n,v,s 1 com rd one?s complement rd $ff ? rd z,c,n,v,s 1 neg rd two?s complement rd $00 ? rd z,c,n,v,s,h 1 sbr rd,k set bit(s) in register rd rd v k z,n,v,s 1 cbr rd,k clear bit(s) in register rd rd ? ($ffh - k) z,n,v,s 1 inc rd increment rd rd + 1 z,n,v,s 1 dec rd decrement rd rd ? 1 z,n,v,s 1 tst rd test for zero or minus rd rd ? rd z,n,v,s 1 clr rd clear register rd rd rd z,n,v,s 1 ser rd set register rd $ff none 1 branch instructions rjmp k relative jump pc pc + k + 1 none 2 ijmp indirect jump to (z) pc(15:0) z, pc(21:16) 0 none 2 rcall k relative subroutine call pc pc + k + 1 none 3/4 icall indirect call to (z) pc(15:0) z, pc(21:16) 0 none 3/4 ret subroutine return pc stack none 4/5 reti interrupt return pc stack i 4/5 cpse rd,rr compare, skip if equal if (rd = rr) pc pc + 2 or 3 none 1/2/3 cp rd,rr compare rd ? rr z, c,n,v,s,h 1 cpc rd,rr compare with carry rd ? rr ? c z, c,n,v,s,h 1 cpi rd,k compare with immediate rd ? k z, c,n,v,s,h 1 sbrc rr, b skip if bit in register cleared if (rr(b)=0) pc pc + 2 or 3 none 1/2/3 sbrs rr, b skip if bit in register is set if (rr(b)=1) pc pc + 2 or 3 none 1/2/3 sbic a, b skip if bit in i/o register cleared if (i/o(a,b)=0) pc pc + 2 or 3 none 1/2/3 sbis a, b skip if bit in i/o register is set if (i/o(a,b)=1) pc pc + 2 or 3 none 1/2/3 brbs s, k branch if status flag set if (sreg(s) = 1) then pc pc+k + 1 none 1/2 brbc s, k branch if status flag cleared if (sreg(s) = 0) then pc pc+k + 1 none 1/2 breq k branch if equal if (z = 1) then pc pc + k + 1 none 1/2 brne k branch if not equal if (z = 0) then pc pc + k + 1 none 1/2 brcs k branch if carry set if (c = 1) then pc pc + k + 1 none 1/2 brcc k branch if carry cleared if (c = 0) then pc pc + k + 1 none 1/2 brsh k branch if same or higher if (c = 0) then pc pc + k + 1 none 1/2 brlo k branch if lower if (c = 1) then pc pc + k + 1 none 1/2 brmi k branch if minus if (n = 1) then pc pc + k + 1 none 1/2 brpl k branch if plus if (n = 0) then pc pc + k + 1 none 1/2 brge k branch if greater or equal, signed if (n v= 0) then pc pc + k + 1 none 1/2 brlt k branch if less than zero, signed if (n v= 1) then pc pc + k + 1 none 1/2 brhs k branch if half carry flag set if (h = 1) then pc pc + k + 1 none 1/2 brhc k branch if half carry flag cleared if (h = 0) then pc pc + k + 1 none 1/2 brts k branch if t flag set if (t = 1) then pc pc + k + 1 none 1/2 brtc k branch if t flag cleared if (t = 0) then pc pc + k + 1 none 1/2 brvs k branch if overflow flag is set if (v = 1) then pc pc + k + 1 none 1/2 brvc k branch if overflow flag is cleared if (v = 0) then pc pc + k + 1 none 1/2 brie k branch if interrupt enabled if ( i = 1) then pc pc + k + 1 none 1/2 brid k branch if interrupt disabled if ( i = 0) then pc pc + k + 1 none 1/2 bit and bit-test instructions lsl rd logical shift left rd(n+1) rd(n), rd(0) 0 z,c,n,v,h 1 lsr rd logical shift right rd(n) rd(n+1), rd(7) 0 z,c,n,v 1 rol rd rotate left through carry rd(0) c,rd(n+1) rd(n),c rd(7) z,c,n,v,h 1 ror rd rotate right through carry rd(7) c,rd(n) rd(n+1),c rd(0) z,c,n,v 1 asr rd arithmetic shift right rd(n) rd(n+1), n=0..6 z,c,n,v 1 swap rd swap nibbles rd(3..0) rd(7..4),rd(7..4) rd(3..0) none 1 bset s flag set sreg(s) 1 sreg(s) 1 bclr s flag clear sreg(s) 0 sreg(s) 1
206 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 sbi a, b set bit in i/o register i/o(a, b) 1 none 1 cbi a, b clear bit in i/o register i/o(a, b) 0 none 1 bst rr, b bit store from register to t t rr(b) t 1 bld rd, b bit load from t to register rd(b) t none 1 sec set carry c 1c1 clc clear carry c 0 c 1 sen set negative flag n 1n1 cln clear negative flag n 0 n 1 sez set zero flag z 1z1 clz clear zero flag z 0 z 1 sei global interrupt enable i 1i1 cli global interrupt disable i 0 i 1 ses set signed test flag s 1s1 cls clear signed test flag s 0 s 1 sev set two?s complement overflow. v 1v1 clv clear two?s complement overflow v 0 v 1 set set t in sreg t 1t1 clt clear t in sreg t 0 t 1 seh set half carry flag in sreg h 1h1 clh clear half carry flag in sreg h 0 h 1 data transfer instructions mov rd, rr copy register rd rr none 1 ldi rd, k load immediate rd k none 1 ld rd, x load indirect rd (x) none 1/2 ld rd, x+ load indirect and post-increment rd (x), x x + 1 none 2 ld rd, - x load indirect and pre-decrement x x - 1, rd (x) none 2/3 ld rd, y load indirect rd (y) none 1/2 ld rd, y+ load indirect and post-increment rd (y), y y + 1 none 2 ld rd, - y load indirect and pre-decrement y y - 1, rd (y) none 2/3 ld rd, z load indirect rd (z) none 1/2 ld rd, z+ load indirect and post-increment rd (z), z z+1 none 2 ld rd, -z load indirect and pre-decrement z z - 1, rd (z) none 2/3 lds rd, k store direct from sram rd ( k) none 1 st x, rr store indirect (x) rr none 1 st x+, rr store indirect and post-increment (x) rr, x x + 1 none 1 st - x, rr store indirect and pre-decrement x x - 1, (x) rr none 2 st y, rr store indirect (y) rr none 1 st y+, rr store indirect and post-increment (y) rr, y y + 1 none 1 st - y, rr store indirect and pre-decrement y y - 1, (y) rr none 2 st z, rr store indirect (z) rr none 1 st z+, rr store indirect and post-increment. (z) rr, z z + 1 none 1 st -z, rr store indirect and pre-decrement z z - 1, (z) rr none 2 sts k, rr store direct to sram (k) rr none 1 in rd, a in from i/o location rd i/o (a) none 1 out a, rr out to i/o location i/o (a) rr none 1 push rr push register on stack stack rr none 2 pop rd pop register from stack rd stack none 2 mcu control instructions break break (see specific descr. for break) none 1 nop no operation none 1 sleep sleep (see specific descr. for sleep) none 1 wdr watchdog reset (see specific descr. for wdr) none 1 mnemonics operands description operation flags #clocks
207 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 24. ordering information notes: 1. code indicators: z h: nipdau lead finish z u: matte tin z r: tape & reel 2. all packages are pb-free, halide-free and fully green and they comply with the european directive for restriction of hazardous substances (rohs). 3. topside marking for attiny20: z 1st line: t20 z 2nd & 3rd line: manufacturing data 4. these devices can also be supplied in wafer form. please contact your local atmel sales office for detailed ordering information and minimum quantities. 24.1 attiny20 speed supply voltage temperature range package (2) ordering code (1) 12 mhz 1.8 ? 5.5v industrial (-40 c to +85 c) (4) 12u-1 attiny20-uur 14s1 attiny20-ssu attiny20-ssur 14x attiny20-xu attiny20-xur 15cc1 attiny20-ccu attiny20-ccur 20m2 attiny20-mmh (3) attiny20-mmhr (3) package type 12u-1 12-ball 1.540 x 1.388mm body, 0.433 mm thick, 0.40 mm pitch (3x4 staggered array), wlcsp 14s1 14-lead, 0.150" wide body, plastic gull wing small outline package (soic) 14x 14-lead, 4.4 mm body, thin shrink small outline package (tssop) 15cc1 15-ball (4 x 4 array), 0.65 mm pitch, 3.0 x 3.0 x 0.6 mm, ultra thin fine-pitch ball grid array package (ufbga) 20m2 20-pad, 3 x 3 x 0.85 mm body, very thin quad flat no lead package (vqfn)
208 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 25. packaging information 25.1 12u-3  
                                       
209 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 25.2 14s1 title drawing no. rev. package drawing contact: packagedrawings@atmel.com 14s1 , 14-lead, 0.150" wide body, plastic gull wing small outline package (soic) 2/5/02 14s1 a a1 e l side view top view end view h e b n 1 e a d common dimensions (unit of measure = mm/inches) symbol min nom max note notes: 1. this drawing is for general information only; refer to jedec drawing ms-012, variation ab for additional information. 2. dimension d does not include mold flash, protrusions or gate burrs. mold flash, protrusion and gate burrs shall not exceed 0.15 mm (0.006") per side. 3. dimension e does not include inter-lead flash or protrusion. inter-lead flash and protrusions shall not exceed 0.25 mm (0.010") per side. 4. l is the length of the terminal for soldering to a substrate. 5. the lead width b, as measured 0.36 mm (0.014") or greater above the seating plane, shall not exceed a maximum value of 0.61 mm (0.024") per side. a 1.35/0.0532 C 1.75/0.0688 a1 0.1/.0040 C 0.25/0.0098 b 0.33/0.0130 C 0.5/0.0200 5 d 8.55/0.3367 C 8.74/0.3444 2 e 3.8/0.1497 C 3.99/0.1574 3 h 5.8/0.2284 C 6.19/0.2440 l 0.41/0.0160 C 1.27/0.0500 4 e 1.27/0.050 bsc
210 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 25.3 14x title drawing no. rev. . . 14x (formerly "14t") , 14-lead (4.4 mm body) thin shrink small outline package (tssop) b 14x 05/16/01 5.10 (0.201) 4.90 (0.193) 1.20 (0.047) max 0.65 (.0256) bsc 0.20 (0.008) 0.09 (0.004) 0.15 (0.006) 0.05 (0.002) index mark 6.50 (0.256) 6.25 (0.246) seating plane 4.50 (0.177) 4.30 (0.169) pin 1 0.75 (0.030) 0.45 (0.018) 0o~ 8o 0.30 (0.012) 0.19 (0.007) dimensions in millimeters and (inches). controlling dimension: millimeters. jedec standard mo-153 ab-1. packagedrawings@atmel.com package drawing contact:
211 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 25.4 15cc1 title drawing no. gpc rev. package drawing contact: packagedrawings@atmel.com c cbc 15cc1, 15-ball (4 x 4 array), 3.0 x 3.0 x 0.6 mm package, ball pitch 0.65 mm, ultra thin, fine-pitch ball grid array package (ufbga) 15cc1 07/06/10 a C C 0.60 a1 0.12 C C a2 0.38 ref b 0.25 0.30 0.35 1 b1 0.25 C C 2 d 2.90 3.00 3.10 d1 1.95 bsc e 2.90 3.00 3.10 e1 1.95 bsc e 0.65 bsc common dimensions (unit of measure = mm) symbol min nom max note top view 123 4 a b c d e d 15-?b d c b a pin#1 id 0.08 a1 a d1 e1 a2 a1 ball corner e 123 4 side view b1 bottom view e note1: dimension ?b? is measured at the maximum ball dia. in a plane parallel to the seating plane. note2: dimension b1 is the solderable surface defined by the opening of the solder resist layer.
212 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 25.5 20m2 title drawing no. gpc rev. package drawing contact: packagedrawings@atmel.com 20m2 zfc b 20m2, 20-pad, 3 x 3 x 0.85 mm body, lead pitch 0.45 mm, 1.55 x 1.55 mm exposed pad, thermally enhanced plastic very thin quad flat no lead package (vqfn) 10/24/08 15 14 13 12 11 1 2 3 4 5 16 17 18 19 20 10 9 8 7 6 d2 e2 e b k l pin #1 chamfer (c 0.3) d e side view a1 y pin 1 id bottom view top view a1 a c c0.18 (8x) 0.3 ref (4x) common dimensions (unit of measure = mm) symbol min nom max note a 0.75 0.80 0.85 a1 0.00 0.02 0.05 b 0.17 0.22 0.27 c 0.152 d 2.90 3.00 3.10 d2 1.40 1.55 1.70 e 2.90 3.00 3.10 e2 1.40 1.55 1.70 e C 0.45 C l 0.35 0.40 0.45 k 0.20 C C y 0.00 C 0.08
213 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 26. errata the revision letters in this section refer to the revision of the corresponding attiny20 device. 26.1 rev. a issue: lock bits re-programming resolution: attempt to re-program lock bits to present, or lower protection level (tampering attempt), causes erroneously one, random line of flash program memory to get erased. the lock bits will not get changed, as they should not. workaround: do not attempt to re-program lock bits to present, or lower protection level. issue: miso output driver is not disabled by slave select ( ss) signal resolution: when spi is configured as a slave and the miso pin is configured as an output the pin output driver is constantly enabled, even when the ss pin is high. if other slave devices are connected to the same miso line this behavior may cause drive contention. workaround: monitor ss pin by software and use the ddrb2 bit of ddrb to control the miso pin driver.
214 attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 27. datasheet revision history revision date comments 8235f 09/2014 changed text in section 25.1 from 12u-1 to 12u-3. updated back page. 8235e 03/13 updated wlcsp ball configuration on page 3 . updated wlcsp package drawing, ?12u-3? on page 208 8235d 10/12 updated document template, and ?pin configurations? on page 2 8235c 06/12 updated ?ordering information? on page 207 . added wafer level chip scale package ?12u-3? on page 208 . 8235b 04/11 removed preliminary status. updated bit syntax throughout the datasheet, e.g. from cs02:0 to cs0[2:0], idle mode description on page 6 , ?capacitive touch sensing? on page 7 (section updated and moved), ?disclaimer? on page 7 , sentence on low impedance sources in ?analog input circuitry? on page 116 , description on 16-bit registers on page 9 , description on stack pointer on page 10 , list of active modules in ?idle mode? on page 23 , description on reset pulse width in ?watchdog reset? on page 30 , program code on page 37 , bit description in figure 11-3 on page 62 , section ?compare output mode and waveform generation? on page 63 , signal descriptions in figure 11-5 on page 64 , and figure 11-7 on page 67 , equations on page 65 , page 66 , and page 67 , terminology in sections describing extreme values on page 66 , and page 67 , description on creating frequency waveforms on page 67 , signal routing in figure 12- 1 on page 76 , top definition in table 12-1 on page 77 , signal names in figure 12-3 on page 79 , twshe bit description in ?twscra ? twi slave control register a? on page 143 , spi slave assembly code example on page 129 , table 21-1 on page 174 , section ?speed? on page 168 , characteristics in figure 21-3 on page 176 , and figure 21-8 on page 179 . added note on internal voltage reference in table 15-4 on page 121 , pradc in table 21-2 on page 175 , miso output driver errata for device rev. a in ?errata? on page 213 8235a 03/10 initial revision
i attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 table of contents features. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1. pin configurations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.1 soic & tssop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.2 vqfn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.3 ufbga . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.4 wafer level chip scale package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.5 pin description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 2. overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 3. general information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 3.1 resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 3.2 code examples. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 3.3 capacitive touch sensing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 3.4 data retention . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 3.5 disclaimer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 4. cpu core . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 4.1 architectural overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 4.2 alu ? arithmetic logic unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 4.3 status register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 4.4 general purpose register file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 4.5 stack pointer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 4.6 instruction execution timing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 4.7 reset and interrupt handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 4.8 register description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 5. memories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 5.1 in-system re-programmable flash program memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 5.2 data memory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 5.3 i/o memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 6. clock system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 7 6.1 clock subsystems. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 6.2 clock sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 6.3 system clock prescaler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 6.4 starting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 6.5 register description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 7. power management and sleep modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 7.1 sleep modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 7.2 software bod disable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 7.3 power reduction register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 7.4 minimizing power consumption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 7.5 register description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 8. system control and reset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 8.1 resetting the avr. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 8.2 reset sources. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
ii attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 8.3 internal voltage reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 8.4 watchdog timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 8.5 register description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 9. interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 9.1 interrupt vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 9.2 external interrupts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 9.3 register description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 10. i/o ports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 10.1 overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 10.2 ports as general digital i/o . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 10.3 alternate port functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 10.4 register description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 11. 8-bit timer/counter0 with pwm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 11.1 features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 11.2 overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 11.3 clock sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 11.4 counter unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 11.5 output compare unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 11.6 compare match output unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 11.7 modes of operation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 11.8 timer/counter timing diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 11.9 register description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 12. 16-bit timer/counter1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 12.1 features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 12.2 overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 12.3 timer/counter clock sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 12.4 counter unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 12.5 input capture unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 12.6 output compare units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 12.7 compare match output unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 12.8 modes of operation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 12.9 timer/counter timing diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.10 accessing 16-bit registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 12.11 register description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 13. timer/counter prescaler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 13.1 prescaler reset. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 13.2 external clock source. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 13.3 register description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 14. analog comparator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 14.1 analog comparator multiplexed input. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 14.2 register description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 15. analog to digital converter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 15.1 features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 15.2 overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 15.3 operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
iii attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 15.4 starting a conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 15.5 prescaling and conversion timing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 15.6 changing channel or reference selection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 15.7 adc noise canceler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 15.8 analog input circuitry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 15.9 noise canceling techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 15.10 adc accuracy definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 15.11 adc conversion result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 15.12 temperature measurement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 15.13 register description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 16. spi ? serial peripheral interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 16.1 features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 16.2 overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 16.3 ss pin functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 16.4 data modes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 16.5 register description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 17. twi ? two wire slave interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 17.1 features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 17.2 overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 17.3 general twi bus concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 17.4 twi slave operation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 17.5 register description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 18. programming interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 18.1 features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 18.2 overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 18.3 physical layer of tiny programming interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 18.4 access layer of tiny programming interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 18.5 instruction set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 18.6 accessing the non-volatile memory controller. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 18.7 control and status space register descriptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 19. memory programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 19.1 features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 19.2 overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 19.3 non-volatile memories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 19.4 accessing the nvm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 19.5 self programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 19.6 external programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 19.7 register description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 20. electrical characteristics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 20.1 absolute maximum ratings* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 20.2 dc characteristics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 20.3 speed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 20.4 clock characteristics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 20.5 system and reset characteristics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 20.6 analog comparator characteristics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 20.7 adc characteristics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 20.8 serial programming characteristics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
iv attiny20 [datasheet] atmel-8235f-avr-attiny20-datasheet_09/2014 21. typical characteristics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 21.1 supply current of i/o modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 21.2 current consumption in active mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 21.3 current consumption in idle mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 21.4 current consumption in power-down mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 80 21.5 current consumption in reset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 21.6 current consumption of peripheral units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 21.7 pull-up resistors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 21.8 output driver strength . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 21.9 input thresholds and hysteresis. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 21.10 bod, bandgap and reset. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 21.11 analog comparator offset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 21.12 internal oscillator speed. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 22. register summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 23. instruction set summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 24. ordering information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 24.1 attiny20 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 25. packaging information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 25.1 12u-3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 25.2 14s1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 25.3 14x . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 25.4 15cc1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 25.5 20m2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 26. errata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 26.1 rev. a . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 27. datasheet revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 table of contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .i
x x x x x x atmel corporation 1600 technology drive, san jose, ca 95110 usa t: (+1)(408) 441.0311 f: (+1)(408) 436.4200 | www.atmel.com ? 2014 atmel corporation. / rev.: atmel-8235f-avr-attiny20-datasheet_09/2014. atmel ? , atmel logo and combinations thereof, enabling unlimited possibilities ? , and others are registered trademarks or trademarks of atmel corporation in u.s. and other countries. other terms and product names may be trademarks of others. disclaimer: the information in this document is provided in connection with atmel products. no license, express or implied, by estoppel or otherwise, to any intellectual property right is granted by this document or in connection with the sale of atmel products. except as set forth in the atmel terms and condit ions of sales located on the atmel website, atmel assumes no liability whatsoever and disclaims any express, implied or statutory warranty relating to its p roducts including, but not limited to, the implied warranty of merchantability, fitness for a particular purpose, or non-infringement. in no event shall atmel be liable for any direct, indirect, consequential, punitive, special or incidental damages (including, without limi tation, damages for loss and profits, business interruption, or loss of information) arising out of the use or inability to use this document, even if atmel has been advised of the possibility of such damages. atmel makes no representations or warranties with respect to the accuracy or c ompleteness of the contents of this document and reserves the right to make changes to specifications and products descriptions at any time without notice. atmel d oes not make any commitment to update the information contained herein. unless specifically provided otherwise, atmel products are not suitable for, and shall not be used in, automo tive applications. atmel products are not intended, authorized, or warranted for use as components in applications intended to support or sustain life. safety-critical, military, and automotive applications disclaimer: atmel products are not designed for and will not be used in connection with any applications where the failure of such products would reasonably be expected to result in significant personal injury or death (?safety-critical a pplications?) without an atmel officer's specific written consent. safety-critical applications include, without limitation, life support devices and systems, equipment or systems for t he operation of nuclear facilities and weapons systems. atmel products are not designed nor intended for use in military or aerospace applications or environments unless specifically designated by atmel as military-grade. atmel products are not designed nor intended for use in automotive applications unless specifically designated by atmel as automotive-grade.


▲Up To Search▲   

 
Price & Availability of ATTINY20-14

All Rights Reserved © IC-ON-LINE 2003 - 2022  

[Add Bookmark] [Contact Us] [Link exchange] [Privacy policy]
Mirror Sites :  [www.datasheet.hk]   [www.maxim4u.com]  [www.ic-on-line.cn] [www.ic-on-line.com] [www.ic-on-line.net] [www.alldatasheet.com.cn] [www.gdcy.com]  [www.gdcy.net]


 . . . . .
  We use cookies to deliver the best possible web experience and assist with our advertising efforts. By continuing to use this site, you consent to the use of cookies. For more information on cookies, please take a look at our Privacy Policy. X